从按类别分隔的帖子类型循环中排除分类

时间:2017-11-08 19:40:19

标签: php wordpress

我正在尝试显示帖子类型的类别并输出该类别中的帖子,但我有另一个要排除的分类。此代码显示注册到特定帖子类型的所有分类和条款。

我试图排除的第二个分类法命名为“制造商”,并注册到多个帖子类型。我试过做'operator'=>'NOT EXISTS'以及你在下面看到的内容,if ...继续但似乎什么都没有用!!

<?php
$post_type = 'equipment';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
 foreach( $terms as $term ):   
if ( $term->slug == 'manufacturer' )
continue; 
  ?>

    <div class="row">
    <div class="col-xs-12">
        <h2><?php echo $term->name; ?></h2>
    </div>

  <div class="col-xs-12">
    <?php
    $args = array(
            'post_type' => $post_type,
            'posts_per_page' => -1,  
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'field' => 'slug',
                    'terms' => $term->slug,
                )
            )

        );
    $posts = new WP_Query($args);
 if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h4><a href="<?php echo get_permalink(); ?>" title="Read more about <?php echo get_the_title(); ?>"><?php  echo get_the_title(); ?></a></h4>
<?php endwhile; endif; ?>
</div>
</div>     
<?php endforeach;
endforeach; ?>

1 个答案:

答案 0 :(得分:0)

$args = array(
    'post_type' => array('equipment'),
    'posts_per_page' => -1,
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'manufacturer',
            'field' => 'slug',
            'operator' => 'NOT IN',
        ),
    ),
);
$query = new WP_Query( $args );