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.
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 🙁
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Sorry for the issues with ACF's API SSL certificate this morning. We've resolved it and your activations and downloads should be back to normal again. Thanks! ^LG— Advanced Custom Fields (@wp_acf) November 2, 2022
© 2022 Advanced Custom Fields.