The below will return the other post in the same category but it will also return the current post too.
Is there a way to exclude the current post from the query?
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxo',
'field' => 'term_id',
'terms' => array(1,2,5),
'operator' => 'IN'
)
)
);
$query = new WP_Query( $args );
The below will return the other post in the same category but it will also return the current post too.
Is there a way to exclude the current post from the query?
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxo',
'field' => 'term_id',
'terms' => array(1,2,5),
'operator' => 'IN'
)
)
);
$query = new WP_Query( $args );
Share
Improve this question
asked Dec 2, 2015 at 8:07
user742736user742736
1773 silver badges7 bronze badges
2 Answers
Reset to default 5Simply add
'post__not_in' => [get_queried_object_id()],
to your array of query arguments. get_queried_object_id()
will return the post ID of the currently viewed single post, and post__not_in
will skip the posts whos ID's was passed as an array to the parameter
You can use post__not_in
with the get_the_ID
of the post in this case your code should be like this :
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxo',
'field' => 'term_id',
'terms' => array(1,2,5),
'operator' => 'IN'
)
),
'post__not_in' => array (get_the_ID()),
);
$query = new WP_Query( $args );
Note: The value has to be an array 'post__not_in' => array (get_the_ID())
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744848776a4597003.html
评论列表(0条)