Site logo

[Support request] How to filter query loop to only include posts that match specific ACF checkbox

Home Forums Support [Support request] How to filter query loop to only include posts that match specific ACF checkbox

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

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2465355
    Alexander

    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!

    #2465843
    David
    Staff
    Customer Support

    Hi there,

    if the field returns a boolean ( true / false ) value then try the method here:

    https://generatepress.com/forums/topic/display-posts-when-an-acf-custom-field-is-present/#post-2444655

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.