最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Complex WP_Query (two post types and multiple operators)

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Apr 27, 2017 at 17:07 pixedelicpixedelic 111 bronze badge 4
  • I tested your query, it is working fine on my end. – BlueSuiter Commented Apr 27, 2017 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 Commented Apr 27, 2017 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. – Tony Djukic Commented Dec 22, 2020 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... – Tony Djukic Commented Dec 22, 2020 at 4:38
Add a comment  | 

2 Answers 2

Reset to default 1

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.

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;
发布评论

评论列表(0)

  1. 暂无评论