Black Friday sale! Get up to 25% off GP Premium! Learn more ➝

[Support request] Query Loop Use “And” instead of “Or” for categories

Home Forums Support [Support request] Query Loop Use “And” instead of “Or” for categories

Home Forums Support Query Loop Use “And” instead of “Or” for categories

  • This topic has 17 replies, 4 voices, and was last updated 12 months ago by Nick.
Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
  • #2431647

    Hi team,

    I’ve searched the knowledge base and can’t find anything on this.

    I love the query loop feature on GP. However, I want to create a query that uses “and” instead of “or”

    For example: I have a blog post categorized in Category1 as well as Category2 (ie: “recipes”+”instant-pot”)

    I want to create a query loop that only draws posts that are in BOTH of those categories. This would require an “Or” request. Otherwise it will draw all recipes and not just the ones in the “Instant Pot” category.

    So how do I create a query that is for “Category1” AND “Category2” in this case, and not “Category1” OR “Category2”.

    I used recipes as an example, but there are thousands of applications where this function would be invaluable.


    Customer Support

    Hi Nick,

    I’m a little bit confused but is this the setup you are looking for?


    Hi Leo,

    Sorry for the confusing ticket.

    In your screenshot example, will that only draw posts that are in BOTH categories “Beaches” and “Destinations”.

    Using that example, I would want a query for “Beaches” and “Thailand”.

    If the query is using “or”, then it would draw all posts about beaches on the site, even if they’re about Greece beaches, which won’t work on a page about Thailand.

    If your example is using “and”, then it would only draw posts that are in the “Thailand” AND “Beaches” category, which would be perfect on a page about Thailand.

    If your example is indeed only drawing posts that appear in BOTH of those categories, then please let me know how to do it! I can’t figure it out 😀

    Customer Support

    Hi Nick,

    1. Add a CSS class to the Grid block under the Query loop block, eg. my-css-class.

    2. Add this PHP snippet:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    	if (
    		! empty( $attributes['className'] ) &&
    		strpos( $attributes['className'], 'my-css-class' ) !== false
    	) {
    		$query_args['tax_query'][0]['operator'] = 'AND';
    	return $query_args;
    }, 10, 2 );

    Adding PHP:


    Hi Ying,

    Thanks for your help with this. I added your PHP Snippet to my functions.php file on the site and I changed ‘my-css-class’ for ‘query-and’

    I then went into the page in question and added a new query loop block. With the query loop selected, I then added a css class on the right size under “advanced” as query-and

    But when I choose two categories (ie: “Kenya” and “Places To Visit”) it shows all the posts in the Kenya category and all the Places To Visit category. I want it to only display posts that are in BOTH the Kenya AND the Places To Visit Category.

    Currently when I add the two categories into the box under “Filters” it shows all posts in the Places To Visit category so that there are posts about Places To Visit in Mexico and other countries appearing on my “Kenya Travel” page that I’m creating.

    I must be missing something. I appreciate your patience.


    I’ve added some login credentials to our staging site to this comment so you can go in and check it out if needed.

    Customer Support

    Hi Nick,

    I see. Can you provide the link and point us to where we can see this specific Query Loop Block?

    Hope to hear from you soon!


    On that link you’ll see part way down the page under: “Indian Cuisine” I have a query loop that uses “India” AND “Food & Drink” as the categories.

    If you look at the query, you’ll see it also has posts about Food in Montana, food in Mexico etc. This is because the query is using “OR” instead of “AND” for the loop. I only want it to draw posts for this loop if the posts are in BOTH “India” and “Food & Drink” categories.

    That way it will make sense on these travel guides.

    Login info added to private section…

    Customer Support

    Hi Nick,

    You’re using a WordPress Query Loop Block instead of a GenerateBlocks Query Loop Block. The code only works for the GB Query Loop Block.

    We have no control over the WordPress Query Loop Block.

    Try replacing it with a GB Query Loop Block instead, and follow the instructions by Ying here:

    Remember to add the class my-css-class to the Classlist of the Grid Block of the GB Query Loop Block as mentioned by Ying.



    Sorry I had tried your method with the GP Query Loop block and it didn’t work at all so I reverted to the WP Query Loop block for that section.

    Please just add your query loop using the method explained above to this post here:

    Scroll down in that edit post screen and find the below text:

    “Posts About Places To Visit in South Africa


    Please replace the content in the hard brackets […] with the actual query loop that will draw only posts in BOTH the “South Africa” and “Places To Visit” categories.

    I can then be sure that it’s possible and copy that block for use on other pages with different category queries.

    This will help me better understand as currently it’s not working using your description.

    You can also check the code I added to the functions.php to ensure it’s correct:

    Thank you

    Customer Support

    I just tested it, and it works:
    Front end:

    Editor query settings:

    Editor CSS class:


    Hi ying,

    Thanks for your help! By copying your block to another post, I was able to recreate it and it worked!

    However, now I’m running into another issue with the Query Loop Block and I have no idea why it’s happening.

    For most categories, your method works great, but for The United States, it simply doesn’t show any posts within that category no matter what I do.

    As you can see, the category has plenty of posts here:

    But if I create a query loop and choose “The United States” as the category, it refuses to show any posts.

    I only have one category on my site called The United States. And yet, even if I make a Query Loop with ONLY the United States Category it refuses to show up.

    Please check this page here as an example:

    I’ve made a Query Loop under the bold text “***GENERATEPRESS STAFF -” and it’s not appearing when viewing or previewing the post.

    Customer Support

    When there’s only 1 category selected, you’ll need to remove the CSS class which calls the custom function from the Grid block of the query loop block.


    Hi Ying,

    I also tried it with 2 categories selected and we have the same exact issue.

    I’ve added 2 now in the example for you to see.

    Please check the above and let me know.



    Customer Support

    It seems working for me when I set the posts per page to 10 instead of 999, I think 999 will cause a timeout.

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