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

2 Answers
2

Simply 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

Leave a Reply

Your email address will not be published. Required fields are marked *