3

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

5

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.