show 10 most recent custom post types excluding the one(s) from specific custom taxonomy
-
22-10-2019 - |
Question
After spending most of the day looking for solutions and trying them out, I decided to explain my problem here.
I am aware that this subject has been covered and as you will see I am using code that has been suggested here and over at ottopress, but I still cannot figure it out.
I have a custom post type called 'review' (non-hierarchical) I have a custom taxonomy called 'reviewcats' (hierarchical) I have a whole bunch of terms under the taxonomy of which 3 are main and the rest children:
- featured
- applications (containing children)
- games (containing children)
On my homepage template I am first showing the latest review (custom post type) that has the term 'featured' from the 'reviewcats' taxonomy. That review also contains the terms 'applications' and a child thereof.
Under the Feature I would like to show the 10 most recent reviews (custom post type), but not the one from above, the one with the term 'featured'
I have been trying with the code below (and many variations of it), but cannot get it right. Can anyone shed light on this dark matter for me please? Much appreciated!
$recentapps_query['tax_query'] = array(
'relation' => 'AND',
array(
'post_type' => 'review',
'post_status' => 'publish',
'posts_per_page' => 10,
),
array(
'taxonomy' => 'reviewcats',
'terms' => array('featured'),
'field' => 'slug',
'operator' => 'NOT IN',
),
);
query_posts($recentapps_query);
if needed, the sandbox site is http://senl.in/jOaalP
Solution
Firstly, your tax query is a little jumbled. The first nested array should actually be at the 'root' of your query_posts()
argument, with tax_query
as a key among them;
array(
'post_type' => 'review',
'post_status' => 'publish',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'reviewcats',
'terms' => array( 'featured' ),
'field' => 'slug',
'operator' => 'NOT IN',
)
)
);
However, forgive me if I'm mistaken, but I think your query could be a lot simpler - simply exclude the 'featured' review you've just grabbed;
query_posts( array(
'post_type' => 'review',
'post_status' => 'publish',
'post__not_in' => array( $post->ID ) // $post *should* be the featured review, since it was the last post queried/worked on
) );