Wordpress post__not_in在WP_Query循环中不起作用

时间:2019-05-23 14:59:53

标签: wordpress

我只需要获得特定类别的相关职位。但是'post__not_in'=>数组(get_the_ID(),4)无法正常工作。

    <?php

      $cats_to_ignore = array(get_the_ID(), 4);
      $categories = wp_get_post_categories( get_the_ID() );
      $category_in = array_diff( $categories, $cats_to_ignore );

      if( count( $category_in ) == 0 ) {
            $category_in = $categories;
        }
        $cat_args = array(
            'category__in'   => $category_in,
            'posts_per_page' => 14,
            'orderby'        => 'date',
            'post__not_in'   => array(get_the_ID(), 4)
            );
        $cat_query = new WP_Query( $cat_args );
        while ( $cat_query->have_posts() ) : $cat_query->the_post();
            /* just example markup for related posts */
            echo '<h2><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h2>';
        endwhile;
        // reset $post after custom loop ends (if you need the main loop after this point)
    wp_reset_postdata(); ?>

它将显示所有相关的帖子,但不排除“ post__not_in”中的类别

0 个答案:

没有答案