Support

Account

Home Forums Front-end Issues Display ACF fields that are modified by filters

Solving

Display ACF fields that are modified by filters

  • I am dynamically loading data into my field by using a filter on the field like this:

    add_filter('acf/load_field/name=custom_field','acf_custom_field_choices', 10, 1);

    When I try and display the field, it is displaying the original data from the field that I am now overwriting with the filter.

    Here is how I display the field:

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

    Is there a better way to display a field value that’s data is being loaded via a filter, since it is not altering the original data of the field?

  • You are using a filter to alter the choices of the field and not the value of the field. This makes your question confusing.

    Also, since this is some type of selection field, it is likely that the value returned by get_post_meta is an array, but this is a guess. Some select type fields store array values.

  • 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?

  • Dynamically loaded choices should not make a difference. Those dynamic choices should also be populated when you call get_field_object()

    What type of field is it and what are the field settings?

    To see what you’re working with

    
    $field = get_field_object( 'custom_field' );
    echo '<pre>'; var_dump($field); echo '</pre>';
    $value = get_post_meta( $post_id, 'custom_field', true );
    echo '<pre>'; var_dump($value); echo '</pre>';
    echo $field['choices'][ $value ];
    
  • 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, priority can matter, try 20.

  • 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.

  • Please provide the output from dumping the field and the value.

  • $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
  • Also, I should have originally mentioned, I am using ACF Pro v5.9.1.

  • Your issue is that that choice value is “Blue” and the value of the field is “blue”. These are not the same due to the letter case of the “B”

  • 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.

  • The value in your code is set here $value = $page->post_name; so this is dependent on what WP is returning as thepost_name. Try $value = strtolower($page->post_name); I have no idea why it’s returning a capitalized value unless that what’s in the DB.

  • 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.

  • To be honest, I’m a little confused whey your are doing this

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

    Why get the field object, then get the meta value and then output echo the label.

    Set the field to either return the label or to return and array with value/label and then use

    
    // set to return label
    the_field('custom_field')
    

    or

    
    // set to return both
    $value = get_field('custom_field');
    echo $value['lable'];
    

    back to your question. If the values choices are not being set when you call get_field_object() then that means that your filter is not being called. Where/in what file/when is your load filter being called?

  • 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 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?

  • When using a sub field always use the field key when dynamically populating choices or when getting the field object.

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

You must be logged in to reply to this topic.