簡體   English   中英

從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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM