从WP_Query中排除特定条款

时间:2017-02-06 10:30:48

标签: php wordpress loops taxonomy-terms

我想为分类术语创建一个foreach循环,用于自定义帖子类型。

更具体地说,我想要一个查询所有产品类别的循环,而不是类别"特别优惠"而不是类别子类别。奖金将是if,产品没有类别查询它们并按ASC顺序排序所有这些(不像分类产品和类别。所有这些都必须同时排序)。

那么我应该如何处理我的代码以使其按需运行?

当前代码:

<?php

$args = array(
    'post_type'    => 'products',
    'showposts'    => -1,
    'post_status'  => 'publish',
    'parent' => 0,
    'hide_empty' => true,
    'tax_query' => array(
        'taxonomy' => 'categories',
        'field'    => 'slug',
        'terms'    => array( 'special-offers', 'other-terms' ),
        'operator' => 'NOT IN',
    ),
);

$terms = get_terms('categories', $args );

foreach ( $terms as $term ) :

    echo '<h2>' . $term->name . '</h2>';

endforeach; 

?>

2 个答案:

答案 0 :(得分:1)

您的Tax Query应该在另一个数组中查找。

'tax_query' => array(
   array(
    'taxonomy' => 'categories',
    'field'    => 'slug',
    'terms'    => array( 'special-offers', 'other-terms' ),
    'operator' => 'NOT IN',
   )
),

其余部分似乎没问题。 查看此

上的WP_Codex

答案 1 :(得分:1)

最终解决方案是将exclude和term id添加到分类法参数中。因为它是用于分类法而且它使用foreach循环。

$args = array(
    'parent' => 0,
    'hide_empty' => true,
    'exclude' => 13,
);

可以在此处找到如何输出没有分类的自定义帖子类型帖子的答案:http://www.codeforest.net/wordpress-tip-show-posts-no-category-term

感谢CBroe和Ste的时间。

相关问题