Support

Account

Home Forums Feature Requests Default value for the taxonomy field type

Solving

Default value for the taxonomy field type

    • wube

    • March 4, 2016 at 9:25 am

    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.

    web inspector for Post Edit admin

    
    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?

    • andrnimm

    • February 10, 2017 at 10:39 am

    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.

  • @hube2 Your load_value version was spot on. Good work!

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.