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.

Query Loop Taxonomies AND, OR logic

  • Hi there,

    Current Query Loop work:
    Currently, when the Tanoxomies/Tags option is selected in the Select Query Parameter field, and for example, two tags are selected in the Tags field, the loop displays posts that have either the first or the second tag (it works with an OR condition). But I want it to show only posts that have both tags added. In other words, I want the condition to work with an AND logic. Unfortunately, I don’t see such settings in the query loop itself. Is it possible to achieve this with additional PHP code?

    Thank you.

  • Hi there,

    Try this:

    1. Add an CSS class to the Grid block of the query loop, eg. my-css-class.
    Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    2. Add this PHP code to set up the AND logic:

    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/

  • Perfect! Thanks.

  • No Problem 🙂

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