Support

Account

Home Forums General Issues Not save empty fields in database

Solved

Not save empty fields in database

  • I have 10 CPTs with 15 ACF fields on average for each CPT.
    I noticed that all the empty form ACF fields are saved in the database making wp_postmeta table very large and bloated.
    It makes querying the wp_postmeta table pretty slow with all those empty fields in there.

    Is there a way to prevent empty ACF fields from being saved/inserted in the database?

  • There is nothing built into ACF that will do this. However, you could modify what ACF is doing by removing empty values from the input. You could create an acf/save_post filter that runs on priority 1 (before acf) and filter $_POST[‘acf’] to remove any empty values. But there would be an issue with this, for example, what if a field had a value previously and not does not have a value. You’d also need to check for existing values in fields that are empty when submitted but used to have a value and then delete the existing value yourself.

  • You’d also need to check for existing values in fields that are empty when submitted but used to have a value and then delete the existing value yourself.

    A very good point. Thank you

  • Hello,

    did you find a solution for this?

    I need to do exactly the same thing as I have a lot of repeater fields and flexible content which is blowing my database.. but I cannot find a solution.

    My first Idea was to find empty fields and check for the saved value and unset it in $_POST[‘acf’] if it was empty before as well. But I had to abandon this because it did not save if somebody deleted a repeater field.

    My second idea then was to get 2 arrays old and new and compare the values
    but this is not working because one uses keys the other names and the structure is not the same 🙁

  • The solution I went for is to store the ACF data in custom database tables because saving all the CPTs metadata in wp_postmeta doesn’t t scale especially if you need to do reporting on the data.
    The problem and the solution are well explained here

  • Unless you are doing some sorting by meta, there’s no reason for fields to be stored in wp_postmeta. I’m gonna try this solution on a test site. Thanks @cr101

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Not save empty fields in database’ is closed to new replies.