从wp_query中排除自定义分类


exclude a custom taxonomy from wp_query

在我的WP网站中,我有2个类别和一些类似的帖子。

cat_1- post 1, post 2, post 3.
cat_2- post 2, post 3, post 4.

当我在帖子 3 页面时,我只想显示类别 2 中的已删除文章。可能我没有抓住逻辑。任何帮助将不胜感激。

<?php
$terms        = get_the_terms( $post_id, 'category' );
if( empty( $terms ) ) $terms = array();
$term_list    = wp_list_pluck( $terms, 'slug' );
$related_args = array(
    'post_type'      => 'post',
    'posts_per_page' => -1,
    'post_status'    => 'publish',
    'post__not_in'   => array( get_the_ID() ),
    'orderby'        => 'desc',
    'tax_query'      => array(
    'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => $term_list
        ),
        array(
            'taxonomy' => 'category',
            'terms' => array('cat_1'),
            'field' => 'slug',
            'operator' => 'NOT IN',
    ),
    ),
);

$related = new WP_Query( $related_args );
if( $related->have_posts() ):
?>
    <div class="post-navigation">
        <h3>Related posts</h3>
        <ul>
            <?php while( $related->have_posts() ): $related->the_post(); ?>
                <li><?php the_title(); ?></li>
            <?php endwhile; ?>
        </ul>
    </div>
<?php
endif;
wp_reset_postdata();
?>

这里是以下查询...可能对您有所帮助...

$custom_tex=array(array('taxonomy'=>'category','field'=>'slug','terms'=>'cat_2'));
$new = new WP_Query(array('post_type'=>'post','posts_per_page'=>-1, 'tax_query'=>$custom_tex,'order' => 'DESC','orderby' => 'ID'  ));
while ($new->have_posts()) : $new->the_post(); 
echo $post->post_title;
echo "<br>";
 endwhile;