e-smartsolution ยป December 11, 2018

Daily Archives: December 11, 2018

Website developer Wordpress

Exclude category WordPress ,WP_Query category__not_in or cat -cat_ID not working

Published by:

Exclude category WordPress ,WP_Query category__not_in or cat -cat_ID not working

Here is the example:

$recent_posts_query_args = array(
‘posts_per_page’ => 5,
‘post_status’ => ‘publish’,
‘order’ => ‘DESC’,
‘post_type’ => ‘product’,
‘category__not_in’=>array(43)
$recent_posts = new WP_Query( $recent_posts_query_args );

It suppose to not include the articles where the category has code 43, but is not working.

The reason is not working is because, the category 43 is not WP category code is tag code 43 for post type “product”, so to do so you will need filter using tax_query

$recent_posts_query_args = array(
‘posts_per_page’ => 5,
‘post_status’ => ‘publish’,
‘order’ => ‘DESC’,
‘post_type’ => ‘product’,
‘tax_query’=>array(array(
‘taxonomy’ => ‘product_cat’,
‘terms’ => 43,
‘field’ => ‘id’,
‘operator’ => ‘NOT IN’,
))

);
$recent_posts = new WP_Query($recent_posts_query_args );