2

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?

  • Are both taxonomies available to both post types? – Milo Dec 14 '17 at 7:17
  • No, they aren't – the "category" taxonomy is available to A, the "portfolio-category" is available to B. – Manu Dec 23 '17 at 21:54
  • 1
    That possibly has something to do with the issue then, WP checks if a taxonomy is available for use with a post type before it generates the query. I would try registering both taxonomies for both post types to see what that does. You can hide the meta boxes on post edit screens so that no one can add terms from those taxonomies. – Milo Dec 24 '17 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.