【问题标题】:Wordpress Custom Post Loop can't exclude categoryWordpress 自定义帖子循环不能排除类别
【发布时间】:2026-01-29 09:15:01
【问题描述】:

我试图从自定义帖子类型循环中排除一个类别,但没有运气。我不知道为什么这不起作用。我查遍了整个互联网,但没有运气,请有人帮忙找出问题所在。

我有一个 ID 为 141 的类别。

我正在运行一个帖子循环,我试图排除分配给该类别的帖子,但它们仍然显示在循环中。我哪里错了?这是我的代码。

<?php 
     
                $args = array(
                'post_type' => 'programme',
                'cat' => -141,
                );


      
        $the_query = new WP_Query( $args ); 

        if ( $the_query->have_posts() ) : 

     
            while ( $the_query->have_posts() ) : $the_query->the_post(); 
          ?>

                <div class="grid-item">  
                <?php the_title(); ?>
                </div>

                <?php 
 
            endwhile; 
        else: 
        
            _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); 
        endif; 

        wp_reset_postdata(); 
        ?>

【问题讨论】:

  • 在您的$args 中更改'cat' =&gt; '-141', 而不是'cat' =&gt; -141,

标签: php wordpress custom-post-type custom-taxonomy


【解决方案1】:

对于任何有类似问题的人,我设法改用此代码来解决它。

$args = array(
                'post_per_page' => '-1',
                    'post_type' => 'programme',
                'tax_query' => array(
                   array(
                   'taxonomy' => 'ticket-category',
                   'field' => 'slug',
                   'terms' => array( 'savers-tickets' ),
                   'operator' => 'NOT IN',
                   )
                   ),
                );

【讨论】:

    最近更新 更多