Support

Account

Forum Replies Created

  • I think I may be on to something here.

    In my example, my custom_field is a grouping/sub field under another field custom_fields.

    If I get the output of:

    print_r( get_field_object( 'custom_fields' ) );

    Then I see the correct key => value designation for the sub_field choices.

    If I get the output of just the custom_field:

    print_r( get_field_object( 'custom_fields_custom_field' ) ); //Using custom_fields_custom_field because custom_fields is a group

    The we are back to the incorrect value => value relationship for the field.

    Is this because I am trying to get the value of the sub_field?

  • Since I am storing the post_title in the meta field, I was using that as the key to match on in the choices array of the field object, so that it would display the label, which is why I thought this was the approach to take:

    $field = get_field_object( 'custom_field) );
    $value = get_post_meta( $post_id, 'custom_field', true );
    echo $field['choices'][ $value ];

    Versus when I try and do this:

    $value = get_field('custom_field')

    Which just returns the key/choice value of blue and the label value of Blue.

  • I had thought about that too, but it didn’t really change anything.

    It’s strange that when I go into the Field Groups and look at choices, it is formatted correctly

    blue : Blue

    And the values are saved to the post meta that way, it just seems that when you try and display the field, the choices are reverting back to the original values and not the custom, dynamic values, that are being brought in via the filter.

  • The choice value *should* be blue with a lower case, based on the custom filter loading the in the dynamic choices. That’s been the problem. The choices for the field are displaying the original field values and not the values being created from the filter.

  • Also, I should have originally mentioned, I am using ACF Pro v5.9.1.

  • $field = get_field_object( 'custom_field' );
    echo '<pre>'; print_r($field['choices']); echo '</pre>';
    //Displays
    Array
    (
        [ID] => 177
        [key] => field_5dcf111592fe2
        [label] => Custom Color
        [name] => custom_field
        [prefix] => acf
        [type] => select
        [value] => blue
        [menu_order] => 0
        [instructions] => 
        [required] => 0
        [id] => 
        [class] => 
        [conditional_logic] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [field] => field_5dcf0ad861cf1
                                [operator] => ==
                                [value] => background
                            )
    
                    )
    
            )
    
        [parent] => 176
        [wrapper] => Array
            (
                [width] => 
                [class] => 
                [id] => 
            )
    
        [choices] => Array
            (
               [Blue] => Blue
               [Red] => Red
               [Green] => Green
               [Yellow] => Yellow
               [White] => White
               [Pink] => Pink
            )
    
        [default_value] => Array
            (
            )
    
        [allow_null] => 1
        [multiple] => 0
        [ui] => 0
        [return_format] => value
        [ajax] => 0
        [placeholder] => 
        [_name] => background
        [_valid] => 1
    )
    $value = get_post_meta( $post_id, 'custom_field', true );
    echo $value;
    //Displays
    blue

    I see the intended choices when I go into edit the custom fields. The select menu shows me:

    blue : Blue
    red : Red
    green : Green
    yellow : Yellow
    white : White
    pink : Pink
  • I have tried different priorities from 20 all the way to 999 and unfortunately no changes were made – I still get the unidentified index error when trying to display the field.

    I took it a step further and tried to display the field on in my template, and the same error occurred.

    I can get the post meta field and display that value, but when I try and place that value in the field choices array, then I get the index error.

  • When I dump out the value for the $field the choices are are the original values that was inserted into the field, and not the filtered values that are being brought in dynamically.

    I am not sure if it matters or not, but I am trying to display the fields as custom columns in my custom post type.

    function custom_column( $column, $post_id ) {
            
        switch ( $column ) {
                
    		case 'custom_title':
                
    			$field = get_field_object( 'custom_field) );
    			$value = get_post_meta( $post_id, 'custom_field', true );
    			echo $field['choices'][ $value ]; //This returns an Undefined index and the index value is not in the $field variable.
    
    			break;
                
            default:
                
                break;
    
        }
    }
    add_action( 'manage_custom_posts_custom_column', 'custom_column', 10, 2);

    And to load my custom field:

    function acf_custom_field_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
    
    	$args = array(
    		'post_type'  => 'custom_post_type',
    		'parent'     => 0
    	);
    	
    	$pages = get_pages( $args );
    	
    	foreach ( $pages as $page ) {
                
    		// vars
    		$value = $page->post_name;
    		$label = $page->post_title;
    
    		// append to choices
    		$field['choices'][ $value ] = $label;
            
        }
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=custom_field','acf_custom_field_choices', 10, 1);

    I am not sure if my loading sequence or priority of the filter and/or action matters.

  • Yes, that’s the issue. I am using the filter to alter the choices and have dynamic selection based on custom post types. I save the choices as key : term for a select field

    However, when I try and use the get_post_meta and grab the key for the selected field, it’s unable to find the matching term since the original choice was filtered in dynamically.

    Does that make any additional since?

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