Home › Forums › Feature Requests › Default value for the taxonomy field type
It would be nice to be able to set a default value for the taxonomy field type
Thread has been marked for the developer when he has time to look at feature requests here.
Is the default value filterable?
I’ve seen this post on filtering default tax field for front end editor, but the filter’s not working for me in the basic back end admin.
I just tried this and the example I gave still seems to be working.
This if for checkbox/multiselect field type
add_filter('acf/load_field/key=field_589c6d5477f05', 'set_tax_default');
function set_tax_default($field) {
$field['default_value'] = array(3,4);
return $field;
}
and this works for radio and single select
add_filter('acf/load_field/key=field_589c6d5477f05', 'set_tax_default');
function set_tax_default($field) {
$field['default_value'] = 3;
return $field;
}
The value needs to match the term ID(s) of the default values you want to set.
Hi John,
I’ve disabled all other plugins and switched to 2017 theme, but it’s still not working – odd!
Here is a screen grab of the web inspector, just to show that i have the relevant field key and default value.
add_filter('acf/load_field/key=field_58086a9b13be5', 'kiteboat_set_tax_default');
function kiteboat_set_tax_default( $field ) {
$field['default_value'] = 304; // that's "private"
return $field;
}
Are you trying to add a default value to a field on a post that was previously save without a value?
If this is an exiting post, does it do the same thing when you add a new post?
What version of ACF are you using?
Thanks John. Using ACF 5.5.7.
This is for adding a new post. I’m not too concerned about previously saved posts, just trying to get the default working for new posts.
screen: http://localhost:8888/projectkiteboat/wp-admin/post-new.php
I think I recreated it. Do you have Load Terms set to yes?
Just checked. Yes i do!
Should I not? and if not how will the postbox get the saved terms?
I would say that you’ll need to go about setting the default value differently in this case. You’re using a custom taxonomy, so that should make things a bit less difficult since your not dealing with “Uncategorized”
Rather than using acf/load_field in this case you should use acf/load_value https://www.advancedcustomfields.com/resources/acfload_value/ with a priority > 10
Going to be honest, I’m not exactly sure what needs to be done, this is a bit of a guess.
add_filter('acf/load_value/key=field_58086a9b13be5', 'kiteboat_set_tax_default', 20, 3);
function kiteboat_set_tax_default( $value, $post_id, $field ) {
if ($value === false && get_post_status($post_id) == 'auto-draft') {
$value = 304;
}
return $value;
}
Thanks, I read through the docu on that filter.
I also tried using acf/prepare_field
. I’m not sure any of these filters is getting fired, though. I have added error_log( print_r ( $field, true ));
into the filter just to check that variable and nothing is appearing in the debug log.
With the acf/load_value
filter I tried using various priorities, and also eliminated the post_status check ( so any false value would trigger the default ) but still nothing’s doing.
What’s the last parameter on the filter, ‘3’?
The last parameter is the number of parameters that WP sends to your filter.
There are only a couple of reasons the filter wouldn’t be running. Either you have the wrong field key or the code is in the wrong place. What file did you put it in?
Oh jeez! I had been modding the wrong functions.php, since i switched to the default theme. I can confirm the load_value
filter fires and works as expected. Thanks for sticking with me.
No problem, I managed to learn something as well, that a filter I thought should work will not when using load and set terms, which is something that I do quite often.
Just adding this here in case someone else needs this. This piece of code will add the first taxonomy term as default for all taxonomy fields *on the frontend*. I needed this for frontend forms, but feel free to remove the “is_admin” check if that’s what you need.
Beware of potential overhead if you have many terms or fields that would be triggered by this.
function set_tax_default($field) {
if(!is_admin()){
$terms = get_terms($field['taxonomy'], ['number' => 1]);
if($terms) {
$single_term = reset($terms);
$field['default_value'] = $single_term->term_id;
}
}
return $field;
}
add_filter('acf/load_field/type=taxonomy', 'set_tax_default');
The topic ‘Default value for the taxonomy field type’ 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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.