Support

Account

Home Forums Add-ons Repeater Field Default value for Taxonomy fields (Add new)

Helping

Default value for Taxonomy fields (Add new)

  • Hi. I need to create and edit tons of custom posts. It would be wonderful if I could select a default value for taxonomy fields, but I see the feature has yet to be added.
    I tried to load a default value with some filters. The first one is a regular checkbox Taxonomy field, and the second one is the same thing, but it’s inside a repeater and a radio button:

    add_filter('acf/load_value/key=field_5821fe811dba6', 'set_tax_libri_default', 20, 3);
    function set_tax_libri_default( $value, $post_id, $field ) {
    	if ($value === false && get_post_status($post_id) == 'auto-draft' && get_post_status($post_id) != 'publish') {
    		$value = 7;
    	}
      return $value;
    }
    
    add_filter('acf/load_value/key=field_5820b0492a8a4',  'set_tax_libri_occorrenza_default', 10, 3);
    function set_tax_libri_occorrenza_default($value, $post_id, $field) {
    	if ($value === false && get_post_status($post_id) == 'auto-draft' && get_post_status($post_id) != 'publish') {
    			$value	= array();
    			$value[] = array('field_5820b05d2a8a5' => 7);
    	}
    	return $value;
    }

    The code works as intended, except if I click on “Add new” under the first instance of the repeater to load a new one, there default value is not loaded. I’m doing something wrong?

    Thanks!

    Edit: it does NOT work as intended. If I edit a published post with a different values, add another one and save the post, all the instances in the repeater are equal to the default value. Can I preserve already published values?

  • Please disregard the “Edit” part. I accidentaly turned on the wrong option in the subfield settings. Now it all works as intended, except for the “Add row” (not “Add new”, my mistake) part.

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

The topic ‘Default value for Taxonomy fields (Add new)’ is closed to new replies.