WP_Query for custom taxonomies showing posts from non-specified terms?

by samar   Last Updated August 17, 2019 05:08 AM

I have two custom taxonomies "products" & "products-info" registered for custom post type "products-posts".

There are three terms (shoes, shirts, pants) in "products". Similarly, there are two terms (color, size) in "products-info".

<?php
$args = array(
    'post_type'             => 'products-posts',
    'posts_per_page'        => 5,
    'orderby'               => 'date',
    'post__not_in'          => array( $post_id ),
    'ignore_sticky_posts'   => 1,
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'products',
            'field'    => 'slug',
            'terms'    => array('shoes'),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'products-info',
            'field'    => 'slug',
            'terms'    => array('color', 'size'),
            'operator' => 'IN',
        )
    )
);

$my_query = new WP_Query( $args );
while( $my_query->have_posts() ) { $my_query->the_post();
    the_title();
}
wp_reset_postdata();
?>

There are 5 posts:

  • post 1 is in three terms (shoes, color, size).
  • post 2 is in one terms (color).
  • post 3 is in one term (size).
  • post 4 is in one term (shirts).
  • post 5 is in one term (pants).

Now I'm visiting/on post 1.

What I'm expecting: there should be related two posts (post 2 & post 3).

What I'm getting is: all 5 posts. As you can see, the terms "shirts & pants" are not specified in query args. Then why it is showing post 4 & post 5.



Related Questions



Show custom taxonomies of post type, table based

Updated December 14, 2017 23:08 PM


tax_query showing no results

Updated August 21, 2017 11:08 AM