I would like to display two different post types in the same query... nothing strange so far. But I would like to declare what taxonomies include and what exclude for both post types, so, for instance, I would like to display posts from the category "16" but that do not belong to "19" as well, and portfolio items from taxonomy "32" that do not belong to "34" at the same time.
I thought this is the right way:
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'tax_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 16 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 19 ),
'operator' => 'NOT IN'
),
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 32 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 34 ),
'operator' => 'NOT IN'
),
),
),
);
but it doesn't work. Any clue on this?
I would like to display two different post types in the same query... nothing strange so far. But I would like to declare what taxonomies include and what exclude for both post types, so, for instance, I would like to display posts from the category "16" but that do not belong to "19" as well, and portfolio items from taxonomy "32" that do not belong to "34" at the same time.
I thought this is the right way:
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'tax_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 16 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 19 ),
'operator' => 'NOT IN'
),
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 32 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 34 ),
'operator' => 'NOT IN'
),
),
),
);
but it doesn't work. Any clue on this?
Share Improve this question asked Apr 27, 2017 at 17:07 pixedelicpixedelic 111 bronze badge 4 |2 Answers
Reset to default 1Ok, according to the codex, if you use tax_query
in your arguments for WP_Query, then it will change the default of post_type
from posts
to any
, BUT if a post_type has exclude_from_search
set to true
then it still won't include it in any
. So since we don't know what the configuration is on the portfolio post_type, I suggest you explicitly instruct the query to search for both posts
and your portfolio_post_type
.
$args = array(
//you'll need to put in the correct post-type for your portfolio items.
//the codex says that if you use 'tax_query' that post_type should default to 'any'
//but for the sake of it, try this...
'post_type' => array( 'posts, portfolio_post_type' ),
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'tax_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 16 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 19 ),
'operator' => 'NOT IN'
),
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 32 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 34 ),
'operator' => 'NOT IN'
),
),
),
);
Here's the codex section that discusses this: https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
Sincerely hope this helps.
Try to run it like this, verify the results. Also, make sure there are enough posts to give you results.
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'tax_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 16 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 19 ),
'operator' => 'NOT IN'
),
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 32 ),
'operator' => 'IN'
),
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array( 34 ),
'operator' => 'NOT IN'
),
),
),
);
$wp_query = new WP_Query();
echo '<pre>';
print_r($wp_query);
exit;
portfolio_category
? Is that exactly what it's registered as and are those ID numbers correct. Not to be pedantic but I've had the smallest little typo in stuff like that stump me for hours. It looks as though it should it work. – Tony Djukic Commented Dec 22, 2020 at 4:32