Site logo

Archived topic

How to filter query loop to only include posts that match specific ACF checkbox

1 reply · Started by Alexander on December 17, 2022

Viewing posts 1–2 of 2

Hi there,

For my custom post type "Custom", I have created a custom ACF checkbox field with the name "temperament" and the key "field_608b188ad99a3".

Now, within a query loop block in one of my posts, I only want to show posts from that custom post type where the specific checkbox value "calm" is selected (there might be other options selected as well).

I have given the query loop block an additional CSS class called "special" and then created the following filter within my functions.php (following earlier help forum articles):

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
	if ( 
            ! is_admin() &&
            // if block has an advanced > additional CSS Class of: special
            ! empty( $attributes['className'] ) && 
            strpos( $attributes['className'], 'special' ) !== false
	) {
            // pass meta_query parameter
            $query_args[ 'meta_query' ] = array(
                'meta_key'          => 'temperament',
                'meta_value'      => 'calm',
				'compare' => 'IN',
            );
	}
	
	return $query_args;
}, 10, 2 );

However, nothing happens within the post (and the query loop). All posts from the specific post type are shown. No filtering is happening. What should I do/change? Thank you!

This archived topic is closed to new replies.