Search code examples
wordpresstaxonomycustom-taxonomytaxonomy-terms

How to query Standard and Video Post Format with additional condition in WordPress?


I was able to figure out how to query both the most recent posts that are of Standard and Video post format with the code below:

    $args = array(
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'posts_per_page'    => 10,
        'tax_query'         => array(
            'relation'    => 'OR',
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video', 'post-format-gallery' ),
                'operator' => 'NOT IN'
            ),
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video' ),
                'operator' => 'IN'
            )
        )
    );

This works great with the OR condition since I am trying to get both standard and video post format. However, I have to add another taxonomy query but using the AND condition. That looks like this:

    $args = array(
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'posts_per_page'    => 10,
        'tax_query'         => array(
            'relation'    => 'OR',
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video', 'post-format-gallery' ),
                'operator' => 'NOT IN'
            ),
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video' ),
                'operator' => 'IN'
            ),
            // This should be and
            array(
                'taxonomy' => 'custom-regions',
                'field' => 'slug',
                'terms' => array( 'region-1', 'region-2' ),
                'operator' => 'IN'
            )
        )
    );

As far as I can see, it is not possible to combine (or is it?) the or and and condition in the tax_query. If it is, is there a way to be able to run this query so I can get both standard or post formats that only fall under certain regions?


Solution

  • You can do something like this.

    $args = array(
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'posts_per_page'    => 10,
        'tax_query'         => array(
            'relation'    => 'AND',
            array(
                'relation'    => 'OR',
                array(
                    'taxonomy' => 'post_format',
                    'field'    => 'slug',
                    'terms'    => array( 'post-format-video', 'post-format-gallery' ),
                    'operator' => 'NOT IN'
                ),
                array(
                    'taxonomy' => 'post_format',
                    'field'    => 'slug',
                    'terms'    => array( 'post-format-video' ),
                    'operator' => 'IN'
                ),
            ),
            array(
                'taxonomy' => 'custom-regions',
                'field' => 'slug',
                'terms' => array( 'region-1', 'region-2' ),
                'operator' => 'IN'
            )
        )
    );