从WP_QUERY参数中排除类别不起作用

时间:2017-07-12 作者:dmoz

这些args中的其他一切都很好。唯一不起作用的是排除uncategorized 类别我两个都试过了\'cat\' => \'-1\', 哪里1 是的IDuncategorized 我要排除的类别,以及tax_query 就像我下面讨论的部分一样:

array(
    \'taxonomy\' => \'category\',
    \'field\'    => \'slug\',
    \'terms\'    => \'uncategorized\',
    \'operator\' => \'NOT IN\',
),
以下是完整代码:

$tourDateArtists = get_terms( 
    array(
        \'taxonomy\'   => \'category\',
        \'hide_empty\' => false,
    )
);
$today = strtotime(\'today\');

foreach ( $tourDateArtists as $tourDateArtist ) {                       
    $tourDateArtist_query = new WP_Query( array(
        \'post_type\' => \'tour-date\',
        // \'posts_per_page\' => -1,
        \'meta_key\'     => \'wpcf-tour-date\',
        \'orderby\'      => \'meta_value_num\',
        \'order\'        => \'ASC\',
        \'tax_query\' => array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'slug\',
                \'terms\'    => array( $tourDateArtist->slug ),
                \'operator\' => \'IN\',
            ),
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'slug\',
                \'terms\'    => \'uncategorized\',
                \'operator\' => \'NOT IN\',
            ),
        ),
        \'meta_query\' => array(
            array(
                \'key\'     => \'wpcf-tour-date\',
                \'value\'   => $today,
                \'compare\' => \'>=\',
                \'type\'    => \'NUMERIC\',
            ),
        ),
    ));

    if ( $tourDateArtist_query->have_posts() ) {
        echo \'<div id="\' . $tourDateArtist->slug . \'" class="has-dates">\';
        echo \'<h2><a href="/artists/\' . $tourDateArtist->slug . \'">\' . $tourDateArtist->name . \'</a></h2>\';
        echo \'<ul class="\' . $tourDateArtist->slug . \'-tour-dates">\';
        while ( $tourDateArtist_query->have_posts() ) {
            $tourDateArtist_query->the_post();
            echo \'<li class="tour-date-item">\';
            echo \'<a href="\' . types_render_field( \'tour-date-link\' ) . \'">\';
            echo \'<div class="tour-date-date">\' . types_render_field( \'tour-date\', array( \'format\' => \'F j, Y g:i a\') ) . \'</div>\';
            echo \'<div class="tour-date-city-state">\' . types_render_field( \'city-state\' ) . \'</div>\';
            echo \'<div class="tour-date-venue">\' . get_the_title() . \'</div>\';
            echo \'</a>\';
            echo \'</li>\';
        }
        echo \'</ul>\';
        echo \'</div>\';

        // echo (\'<a href="/tour-dates#\' . $parentSlug . \'" class="load-more-link">Show More Dates</a>\');

    } else {
        /* show \'no news\' message if no tour dates */
        echo \'<div id="\' . $tourDateArtist->slug . \'" class="no-dates">\';
        echo \'<h2>\' . $tourDateArtist->name . \'</h2>\';
        echo \'<p>\' . $tourDateArtist->name . \' is not on tour.\' . \'</p>\';
        echo \'</div>\';
    }
    $tourDateArtist_query = null;
    wp_reset_postdata();
}
这是一个foreach 但每次都应该测试一下tax_query 正当知道为什么不排除我定义的类别吗:uncategorized?

1 个回复
最合适的回答,由SO网友:dmoz 整理而成

经过多次搜索,我最终找到了一个有效的选项:添加一个exclude 参数设置为get_terms 列表位于顶部。像这样:

$tourDateArtists = get_terms( 
    array(
        \'taxonomy\'   => \'category\',
        \'hide_empty\' => false,
        \'exclude\'    => \'1\',
    )
);
还是不知道为什么两者都没有\'cat\' => \'-1\', 也不是特别的tax_query 我使用的参数正在工作,但上面的参数确实工作,所以它会工作。

结束

相关推荐

Theme for subcategories

我正在使用相同菜单处理类别(城市)和子类别(例如:汽车、动物、房屋)。因此,现在我的类别结构如下所示:- London -- cars -- animals -- houses - Manchester --cars --animals --houses 每个子类别的slug(似乎它们必须是唯一的)的名称如下category_name + subcategory_name 看起来像london-cars, manchester-