Black Friday! 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

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2431647
    Nick

    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.

    Thank-you

    #2431780
    Leo
    Staff
    Customer Support

    Hi Nick,

    I’m a little bit confused but is this the setup you are looking for?
    https://www.screencast.com/t/xwzzSxh5e2wc

    #2431791
    Nick

    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 😀

    #2431955
    Ying
    Staff
    Customer Support

    Hi Nick,

    1. Add a CSS class to the Grid block under the Query loop block, eg. my-css-class.
    https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    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: https://docs.generatepress.com/article/adding-php/

    #2432062
    Nick

    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.

    #2432078
    Nick

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

    #2432109
    Fernando
    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!

    #2437858
    Nick

    https://goatsontheroadcom.bigscoots-staging.com/wp-admin/post.php?post=72668&action=edit

    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…

    #2438081
    Fernando
    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: https://generatepress.com/forums/topic/query-loop-use-and-instead-of-or-for-categories/#post-2431955

    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.

    Reference: https://docs.generateblocks.com/article/query-loop-overview/

    #2441719
    Nick

    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:

    https://goatsontheroadcom.bigscoots-staging.com/wp-admin/post.php?post=72653&action=edit

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

    “Posts About Places To Visit in South Africa

    [GP STAFF ADD QUERY LOOP HERE CATEGORIES “SOUTH AFRICA” + “PLACES TO VISIT”]”

    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:
    https://goatsontheroadcom.bigscoots-staging.com/wp-admin/theme-editor.php?file=functions.php&theme=generatepress_child

    Thank you

    #2441982
    Ying
    Staff
    Customer Support

    I just tested it, and it works:
    Front end:
    https://www.screencast.com/t/B2geSkHW

    Editor query settings:
    https://www.screencast.com/t/B2geSkHW

    Editor CSS class:
    https://www.screencast.com/t/Nifu6IqZCy9c

    #2443430
    Nick

    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: https://goatsontheroadcom.bigscoots-staging.com/category/blogs/north-america/usa/

    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: https://goatsontheroadcom.bigscoots-staging.com/wp-admin/post.php?post=53525&action=edit

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

    #2443475
    Ying
    Staff
    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.

    #2443484
    Nick

    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.

    https://goatsontheroadcom.bigscoots-staging.com/wp-admin/post.php?post=53525&action=edit

    Please check the above and let me know.

    Thanks,

    Nick

    #2443504
    Ying
    Staff
    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.
    https://www.screencast.com/t/ZiS7BSvFPvp

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