Support

Account

Home Forums Backend Issues (wp-admin) [acf/select] missing default value if "stylised UI" is enabled

Solved

[acf/select] missing default value if "stylised UI" is enabled

  • Hi,

    I’m having this issue with the latest ACF version (5.3.3.2):

    Steps to reproduce the issue:
    1. Create an acf/select field
    2. enable “stylised UI” option
    3. set a default value

    Let’s suppose the field appear in a metabox inside the post edit screen.
    The field show no value when first loads instead of the default value.
    The problem is that the stylised dropdown (select2) does not properly show the initial default value.

    I’ve attached a screenshot below

  • I don’t know if this is a bug or not, but I confirmed that this is the way it’s working.

    This has been reported to the developer.

  • This should be fixed in the next release, this message from Elliot…

    I’ll release a new version soon, after updating, please make a change to the select field to ensure it is saved and the default_value setting is updated (which fixes the issue)

  • Hello
    this might be related – I’m trying to populate the default value of a dynamically populated select field – I cannot get it to display the default value – hope somebody can help.

    // Populate the category select field with dynamic values
    add_filter( 'acf/load_field/name=resource_category', 'prefix_acf_load_resource_category_field' );
    function prefix_acf_load_resource_category_field( $field ) {
    		
    	// reset choices and defaults
    	$field[ 'choices' ] 		= array();
    	$field[ 'default_value' ]	= array();
    	
    	$terms = get_terms( 'resource_category', array( 'hide_empty' => false ) );
    	
    	foreach ( $terms as $term ) {
    		
    		$field[ 'choices' ][ $term->slug ] = $term->name;
    		
    		if ( has_term( $term->slug, 'resource_category', get_the_ID() ) )
    			$field[ 'default_value' ][] = $term->slug;
    	
    	}
    	
    	return $field;
    
    };
    
  • @mmjaeger, your problem is that get_the_ID() only works inside the WP loop.

    try

    global $post;
    if ( has_term( $term->slug, 'resource_category', $post->ID ) )

    however, adding a default value to a post that already has a selected category really should not have any effect, since something is already selected, unless I’m missing something, and if it’s a new post there won’t be anything selected so there will be no default value. Like I said, maybe I’m missing it and just not understanding.

  • Thanks for replying Joe
    actually getting the taxonomy terms isn’t the issue – what I’m trying to do is basically simulating the “taxonomy” field using a select field – the reasons for that is that unfortunately the “taxonomy field” doesn’t allow for conditions later on whereas the select field does. I can easily get the terms of the taxonomy and populate the select field but how do I pre-select the terms assigned already to the post when populating the select field – any ideas?

  • Did you try adjusting the code that I posted above?

    To change the currently selected value what you want to do is to look at acf/load_value http://www.advancedcustomfields.com/resources/acfload_value/

    so you’ll have 2 filters, one that sets the choices in load field and the other that sets the values in load value.

    The only problem with this is that the value won’t change should someone choose a different term. What I would do is to not display the taxonomy box on the right, not sure how you’d do that. Then what you’ll need to do is also create an acf/save_post filter http://www.advancedcustomfields.com/resources/acfsave_post/ and also update the terms for the post yourself as well.

  • that did the trick – thank you Joe.

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

The topic ‘[acf/select] missing default value if "stylised UI" is enabled’ is closed to new replies.