Home › Forums › General Issues › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.