Title: query posts and exclude current post
Last modified: August 22, 2016

---

# query posts and exclude current post

 *  [Pete](https://wordpress.org/support/users/perthmetro/)
 * (@perthmetro)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-posts-and-exclude-current-post/)
 * I’ve tried various things like below…
 *     ```
       <?php
       $args = array (
       	'gender' => 'female',
       	'seeking' => 'male-2',
       	'state' => 'south-australia',
       	'posts_per_page' => '4',
               'post__not_in' => array($post->ID),
       	'order' => 'ASC',
       	'orderby' => 'rand',
       );
       ?>
       <?php $query = new WP_Query( $args ); ?>
       <?php
       if ( $query->have_posts() ) {
       	while ( $query->have_posts() ) {
       		$query->the_post();
       ?>
   
       loop stuff goes here
   
       <?php wp_reset_postdata(); ?>
       ```
   
 * But I can’t exclude the current post from appearing in this query (sidebar).
 * What am I doing wrong?
 * Thanks heaps
 * Pete

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [PhPCentre](https://wordpress.org/support/users/phpcentre/)
 * (@phpcentre)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-posts-and-exclude-current-post/#post-5903707)
 * Try this:
    `'post__not_in' => array( get_the_ID() ),`
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-posts-and-exclude-current-post/#post-5903767)
 * what is the result if you either echo `$post->ID` or `get_the_ID()` before your
   code?
 * if you don’t get the ID of your single post, possibly try to add `global $post`
   before your code
 *  [tormorten](https://wordpress.org/support/users/tormorten/)
 * (@tormorten)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-posts-and-exclude-current-post/#post-5903829)
 * Using `get_queried_object()->ID` could also work if you are on a page where the
   post object has been set.
 *  Thread Starter [Pete](https://wordpress.org/support/users/perthmetro/)
 * (@perthmetro)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-posts-and-exclude-current-post/#post-5903871)
 * [@phpcentre](https://wordpress.org/support/users/phpcentre/)… no joy, but thanks.
 * [@alchymyth](https://wordpress.org/support/users/alchymyth/)… thanks, i’m not
   sure I’m putting the `global $post` in the correct place? Could you tell me where
   it goes?
 * [@tormorten](https://wordpress.org/support/users/tormorten/)… thanks, again would
   you be able to apply that for me in my snippet?
 * Thanks all.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-posts-and-exclude-current-post/#post-5903888)
 * > putting the `global $post` in the correct place?
 * try before this line:
    `$args = array (`
 * it is possible that the post ID could have been currupted by whatever code is
   between the ‘normal’ loop and your sidebar…
    did you try to print the post ID
   at the start of your code to see if it is the right one?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘query posts and exclude current post’ is closed to new replies.

## Tags

 * [exclude](https://wordpress.org/support/topic-tag/exclude/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [post__not_in](https://wordpress.org/support/topic-tag/post__not_in/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 4 participants
 * Last reply from: [Michael](https://wordpress.org/support/users/alchymyth/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/query-posts-and-exclude-current-post/#post-5903888)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
