Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Multiple Query loops on search results page?

  • Hi GP,

    I’m using GP and GB pro to create a large site and it’s been really great so far, I’m just getting a bit stuck when it comes to the search results page and using multiple query loops.

    The design calls for search results to be split over different tabs i.e
    Pages(5) | Posts(3) | Products(6)

    Currently, I have achieved this by using 3 Query Loops set to “Inherit query from template” and then using display “none” on the various tabs, but this feels much heavier than it perhaps should be.

    Is there a way to use multiple Query Loop blocks that pull in the search term but are then filtered to different content types (page, post, product) before being output?

    Many thanks
    Chris

  • Hi there,

    not sure on this…. but you can try:

    1. Your Loop Template.
    add the 3 query loops each set to Inherit Query from Template

    2. each Query Loop Block, select the Grid Block and in Advanced > Additional CSS Classes add the respective CSS Class:

    posts-query
    pages-query
    products-query

    3. Add this PHP Snippet to filter the generateblocks_query_loop_args

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'posts-query' ) !== false
        ) {
            // merge meta_key $custom_term slug into tax query
            return array_merge( $query_args, array(
                'post_type' => 'post'
            ) );
        }
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'pages-query' ) !== false
        ) {
            // merge meta_key $custom_term slug into tax query
            return array_merge( $query_args, array(
                'post_type' => 'page'
            ) );
        }
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'products-query' ) !== false
        ) {
            // merge meta_key $custom_term slug into tax query
            return array_merge( $query_args, array(
                'post_type' => 'product'
            ) );
        }
        return $query_args;
    
    }, 10, 2 );
    

    That function should set the the queries post-type to post, page, or product for each loop based on the CSS class it was given.

    But its a punt i am not sure it will work in this instance.

  • Ha, for a punt that was a pretty damn good one…

    Works exactly as expected, and even returns better results than my initial method, thank you David 🙂

  • Well, look at me lol – having one of those days 🙂
    Super happy to hear that!!

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