Hi.
I need to update multiple fields at once (or as quick as possible) because I created a custom field after many posts had been made
I know that values are not shown until I hit save in post edit page(just returns null).
Some posts were updated manually, but there are so many to go…
I want to update only those have not been saved yet.
How can I do it in functions.php (by post type if possible)?
Any help or helpful link would be appreciated.
Regards.
There isn’t an easy way to do this. You will need to
1) Get all the posts of the post type
2) Check to see if the field already has a value
3) update if it doesn’t
Depending on the number of posts involved, this process could very well time out before completion.
The better alternative is to set a default value if the field does not contain a value.
$value = get_field('field_name');
if (!$value) {
$value = 'set some default value';
}
The checking of this may need to be more complex, but that’s the idea.
Alternately you could add an acf/load_value filter to the field and return a default value from this and this would not require altering templates where it’s used https://www.advancedcustomfields.com/resources/acf-load_value/