I have hundreds of posts which exist already.
I create a Checkbox ACF and assign default values.
I specify that this checkbox with it’s default values will display on all posts marked as Category1.
I bulk-edit my posts to Category1.
However, the posts are not updated with the defaults as a result. (this is the problem in question.)
If I go into each post-edit screen individually I can see that the Checkboxes are present and the default values are checked as desired. However, the posts have not been updated to reflect this UNTIL I click “UPDATE” in the individual post-edit screen.
I read somewhere on the site: “Default values are loaded when creating a new post / page.” But these posts already exist. I’m trying to assign their ACF default values by selecting Category1.
How do I get existing posts to update their defaults automatically when I set their category?
Thanks for the q,
The default value is only loaded on the edit screen. It is not saved automatically when you import data.
Sorry, but this is outside the scope of the ACF plugin.
You will need to write some custom code to hook into each post imported and then update the postmeta manually
Since you’ve set the checkbox default value yourself when creating the ACF field you don’t really need access to the array..
If the value is just from one checkbox it will be saved as a string (if I remember correctly) and you can just manually write that out as the info to be saved without accessing the checkbox actual values.. Since it’s the same value as for the default value when ACF loads it it will recognize it as the default being checked while in reality you put it in there manually 🙂
Nc, Ic (I see) .. hoho..
Then I suppose it’s a bit more complicated.. so dynamic that you can’t even retrieve a specific field from the ACF interface?
With get_field_object you should be able to retrieve the specific checkbox with it’s values if that’s what you need.
I’m close to a solution but unfortunately:
$default_values = $my_get_field_object[‘default_value’]
$default_values produces a string with no line breaks! I need the default values as an array so that I can use update_field($my_field_key, $default_values, $post->ID) to update the post.
So, it feels redundant to have to create a DEFAULT POST when there are already default values lying around. But That’s what I’ve done. I’ve created a Default Post (like a default template for this category, and I’ll have to make one for each unique ACF category that I create) which is updated with the default options so that I can copy them whenever the default fields aren’t automatically set. The logic works like this:
If my post is set to Category1 &&
If my post’s ACF checkboxes are not already set
THEN update_field( $my_field_key, $copy_values_from_default_post, $default_post_id )
That’s my solution for today. Seems like it should have been more straightforward. Please let me know if there is a better way.
You say the default values are produced as a string.. I believe they’re like this: default1, default2, default3 etc.
If you need this as an array why not simply use php explode?
$myarray = explode(', ', $default_values);
If the string does not contain a space just remove that from the first parameter of the explode 🙂
My recollection of the field object with a checkbox was an array with each value. But I haven’t had a look at it in some time..
I suppose you could copy the field from the ACF core and make your own addon where you put the values in an array instead so that you can use them as you please
And yea, I just retested it with your explode code to make sure and it no worky. I can’t imagine how this could be useful to anyone as is. And I don’t have time to poke around. I suppose this could be a bug report but Elliot says It’s out of scope so I guess it wouldn’t get looked at?
Or maybe I’m just not understanding something about how the default values are properly accessed.
Well that’s not entirely true.. It is out of the scope of ACF to automatically add default values to posts depending on category.. However if there’s no apparent reason to why the field_objects checkbox values are in one long string you could create a fork on the ACF repo on github and fix it yourself and hope for it to be included in the next version 🙂
Alright, so I’ve created a custom post type where defaults can be set and given a unique name. (eg. the post title = template name)
I have 2 things to figure out now:
1) How to copy all ACF data from a given post to another post. (I know how to do it by individual field, I’m just hoping there’s a way to grab all fields at once and copy them over)
2) How to use ACF on a given Post to select from the available templates.
The topic ‘How do you set default values on a set of posts in bulk?’ is closed to new replies.
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!