skip to Main Content

I am trying to query posts according to their post format. Currently, I have the video, gallery, and standard post formats on my site. It’s quite easy to query these post formats individually. However, I have a combination of tax queries of 10 posts that are standard and video post format, my query fails.

Here’s the code I have right now:

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

$query = new WP_Query( $args );

2

Answers


  1. I think, you should put the 'relation' => 'OR', into the 'tax_query'-Array.

    Login or Signup to reply.
  2. You are adding 'relation' => 'OR' in the wrong place. it should be inside the tax_query array. check the below code.

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

    USEFUL LINKS

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search