Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress - category__not_in not working

Tags:

php

wordpress

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 not working at all some.

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

  query_posts($query);
?>

I've already tried:

'category__not_in' => array( '44' ),
'category__not_in' => array( 44 ),
'category__not_in' => '44',
'category__not_in' => 44,

But nothing works =(

like image 918
Michel Makei Gefuni Avatar asked Mar 23 '16 12:03

Michel Makei Gefuni


People also ask

How do I query custom post type in WordPress?

Querying by Post Type You can query posts of a specific type by passing the post_type key in the arguments array of the WP_Query class constructor.

What is WP_Query?

WP_Query is a PHP class for constructing queries to the WordPress database and returning posts, pages, or other custom objects to render on the page. It allows developers to build complex searches while removing the need to write separate SQL queries.

How do I find post queries in WordPress?

query_posts() is a way to alter the main query that WordPress uses to display posts. It does this by putting the main query to one side, and replacing it with a new query. To clean up after a call to query_posts, make a call to wp_reset_query() , and the original main query will be restored.


1 Answers

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);
?>
like image 200
Prakash Rao Avatar answered Nov 02 '22 11:11

Prakash Rao