2014-12-25 41 views
0

一个自定义分类在我的WP网站,我有2类和几个后像..排除wp_query

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

当我在后3页,我想只显示从类别releted文章这里有我的代码:但它返回空。可能我没有理解逻辑。任何帮助将不胜感激。

<?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(); 
?> 

回答

0

这里是下面的查询......可能对你有帮助...

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