wordpress - category__not_in not working

后端 未结 3 1297
心在旅途
心在旅途 2021-01-14 00:19

I\'m having a problem getting my query function. I need to run the loop, excluding a particular category.

I\'m trying to use category__not_in, but is no

相关标签:
3条回答
  • 2021-01-14 00:54

    Use 'cat' => '-44' in your $args array:

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'cat' => '-44',
        'posts_per_page' => 9,
        'paged' => get_query_var('paged')
    );
    

    It's the way recommended in the WP Codex.

    0 讨论(0)
  • 2021-01-14 00:55

    Thanks guys, it worked thanks to @rnevius

    The problem was in my query, I was using WP_Query() and query_posts().

    I used how reference the WP Codex: https://codex.wordpress.org/Class_Reference/WP_Query

    Below is how my code was at the end:

    <?php
      $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category__not_in' => array( 44 ),
        'posts_per_page' => 9,
        'paged' => get_query_var('paged')
      );
      $query = new WP_Query( $args );
    ?>
    
    <?php
      if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
          $query->the_post();
    ?>
    
    // code
    
    <?php
        }
      } else {
        // no posts found
      }
      wp_reset_postdata();
    ?>
    
    0 讨论(0)
  • 2021-01-14 01:03

    Try using tax_query instead :

    <?php
      $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 9,
        'paged' => get_query_var('paged'),
        'tax_query' => array(
            array(
                'taxonomy' => '<YOUR TAXONOMY NAME>',
                'field'    => 'term_id',
                'terms'    => array( 44 ),
                'operator' => 'NOT IN',
            ),
        ),
    
      );
      $query = new WP_Query( $args );
    
      query_posts($query);
    ?>
    
    0 讨论(0)
提交回复
热议问题