1

I would like to display two different post types in the same query... nothing strange so far. But I would like to declare what taxonomies include and what exclude for both post types, so, for instance, I would like to display posts from the category "16" but that do not belong to "19" as well, and portfolio items from taxonomy "32" that do not belong to "34" at the same time.

I thought this is the right way:

$args = array(
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 16 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 19 ),
                'operator' => 'NOT IN'
            ),
        ),
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 32 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 34 ),
                'operator' => 'NOT IN'
            ),
        ),
    ),
);

but it doesn't work. Any clue on this?

4
  • I tested your query, it is working fine on my end.
    – BlueSuiter
    Apr 27 '17 at 17:20
  • Thank you! However, on my end, it displays the blog posts correctly, but it doesn't display portfolio items at all in this way
    – pixedelic
    Apr 27 '17 at 20:12
  • Is it possible that there's a mismatch in the portfolio_category? Is that exactly what it's registered as and are those ID numbers correct. Not to be pedantic but I've had the smallest little typo in stuff like that stump me for hours. It looks as though it should it work. Dec 22 '20 at 4:32
  • Oh man, ignore my previous comment... ...when you set up args for a query, if you don't specify the post type it defaults to 'posts'. So your query is ONLY searching for posts. See my answer... Dec 22 '20 at 4:38
0

Try to run it like this, verify the results. Also, make sure there are enough posts to give you results.

$args = array(
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 16 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 19 ),
                'operator' => 'NOT IN'
            ),
        ),
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 32 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 34 ),
                'operator' => 'NOT IN'
            ),
        ),
    ),
);
$wp_query = new WP_Query();
echo '<pre>';
print_r($wp_query);
exit;
1
  • Sorry, my bad, I shared the query only, since I thought the next part (new WP_Query etc...) was implicit. Thank you anyway, it doesn't work :-)
    – pixedelic
    Apr 27 '17 at 18:05
0

Ok, according to the codex, if you use tax_query in your arguments for WP_Query, then it will change the default of post_type from posts to any, BUT if a post_type has exclude_from_search set to true then it still won't include it in any. So since we don't know what the configuration is on the portfolio post_type, I suggest you explicitly instruct the query to search for both posts and your portfolio_post_type.

$args = array(
    //you'll need to put in the correct post-type for your portfolio items.
    //the codex says that if you use 'tax_query' that post_type should default to 'any'
    //but for the sake of it, try this...
    'post_type' => array( 'posts, portfolio_post_type' ),
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 16 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 19 ),
                'operator' => 'NOT IN'
            ),
        ),
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 32 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 34 ),
                'operator' => 'NOT IN'
            ),
        ),
    ),
);

Here's the codex section that discusses this: https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters

Sincerely hope this helps.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.