I am attempting to build a tax_query, and I’m unable to make it behave.

What I want to retrieve from the query are two different content types:

  • A) Posts that are in a specific Category
  • B) Portfolio items (custom post type) that are in a specific portfolio-category

I could of course make two different queries and then merge their results… but I was under the impression that WP would allow to express this in a single query:

$args = array(
    'post_type' => array( 'post', 'portfolio' ),
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => $cat,
        ),
        array(
            'taxonomy' => 'portfolio-category',
            'field'    => 'slug',
            'terms'    => $cat,
        ),
      ),
  );

This query should return both A and B, but actually it returns only B (portfolio items). It ignores Posts that are in the $cat category.

However, if I remove any of the following conditions:

  • removing “portfolio” from “post_type”, leaving only posts.
  • removing the ‘portfolio-category’ part of the tax_query.

Then it returns the A result (but obviously not B).

Is there any logical explanation for this? Is it impossible to query at the same time for different post_types, and different taxonomies?

0

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *