Site logo

[Resolved] Display posts when an ACF custom field is present

Home Forums Support [Resolved] Display posts when an ACF custom field is present

Home Forums Support Display posts when an ACF custom field is present

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2444438
    George

    I am display a query loop in a sidebar content template. I would like to display posts from a certain custom post type that have a Tue/False ACF custom field toggled to Yes. Can’t find any settings for this.

    #2444655
    David
    Staff
    Customer Support

    Hi George,

    try this snippet:

    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // Check your logic to apply the filtering only to specific loop
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'my-class-name' ) !== false &&
            is_home() &&
            ! is_admin()
        ) {
            // Merge the current $query_args which contains arguments defined in the editor with your ordering arguments
            return array_merge( $query_args, array(
                'meta_query' => array(
                    array(
                    'key' => 'your_custom_field',
                    'value' => '1',
                    'compare' => '==' 
                    )
                ),
            ) );
        }
    
        return $query_args;
    }, 10, 2 );
    #2444668
    George

    Wahoo, amazing, thanks David!

    #2444714
    David
    Staff
    Customer Support

    Wow – first time lol – those meta_queries normally mess me up lol

    Glad to be of help

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