Support

Account

Home Forums General Issues get_field and dynamic filtering

Unread

get_field and dynamic filtering

  • How can I make get_field to work as well with dynamic filtering as get_post_meta do in the code example below? I’m working with third-party code that uses get_field to fetch values and I can’t modify that code so I need get_field to get along with the dynamic filtering. I’ve also tried using the acf/load_value filter but it was to no avail.

    I’ve a post with ID 89 and an ACF field named “test_text_field” with the value “This text is stored in the ACF field.”.

    
    add_action( 'wp_footer', function() {
        add_filter( 'get_post_metadata', 'filter_get_post_metadata', 10, 5 );
        echo 'Step 1 - Filter added: ' . get_post_meta( 89, 'test_text_field', true ) . '<br>';
        echo 'Step 1 - get_field: ' . get_field( 'test_text_field', 89 ) . '<br>';
        remove_filter( 'get_post_metadata', 'filter_get_post_metadata', 10, 5 );
        echo 'Step 2 - Filter removed: ' . get_post_meta( 89, 'test_text_field', true ) . '<br>';
        echo 'Step 2  - get_field: ' . get_field( 'test_text_field', 89 ) . '<br>';
        add_filter( 'get_post_metadata', 'filter_get_post_metadata', 10, 5 );
        echo 'Step 3 - Filter added: ' . get_post_meta( 89, 'test_text_field', true ) . '<br>';
        echo 'Step 3 - get_field: ' . get_field( 'test_text_field', 89 ) . '<br>';
    } );
    
    function filter_get_post_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
        if ( 'test_text_field' === $meta_key ) {
            $value = array( microtime() );
        }
        return $value;
    }
    
    
    Output:
    Step 1 - Filter added: 0.47892500 1659789061
    Step 1 - get_field: 0.47922900 1659789061
    Step 2 - Filter removed: This text is stored in the ACF field.
    Step 2 - get_field: 0.47922900 1659789061
    Step 3 - Filter added: 0.47933000 1659789061
    Step 3 - get_field: 0.47922900 1659789061
    

    Thankful for any ideas on how to get the dynamic filtering working with get_field.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.