Title: Problem with post__in
Last modified: August 19, 2016

---

# Problem with post__in

 *  [madmaia](https://wordpress.org/support/users/madmaia/)
 * (@madmaia)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/problem-with-post_in/)
 * Hi, I’m having a strange problem with **post__in**. I am trying to create two
   new loops on a page, one to display sticky posts of class ‘events’, the other
   to display non-sticky ‘events’. This is what I’ve got:
 *     ```
       // first create my query and object for sticky events
       		$querySticky = array(
       			'category_name' => 'events',
       			'posts_per_page' => '20',
       			'meta_key' => '_EventStartDate',
       			'meta_compare' => '>=',
       			'meta_value' => $currentTime,
       			'orderby' => 'meta_value',
       			'order' => 'ASC',
       			'post__in' => get_option('sticky_posts')
       		);
       		$queryStickyObject = new WP_Query($querySticky);
       		// now create non-sticky query and object
       		$queryNonSticky = array(
       			'category_name' => 'events',
       			'posts_per_page' => '20',
       			'meta_key' => '_EventStartDate',
       			'meta_compare' => '>=',
       			'meta_value' => $currentTime,
       			'orderby' => 'meta_value',
       			'order' => 'ASC',
       			'post__not_in' => get_option('sticky_posts')
       		);
       		$queryNonStickyObject = new WP_Query($queryNonSticky);
       		// check if there are any events to display
       		if (($queryStickyObject->have_posts()) || ($queryNonStickyObject->have_posts()))	{
       			?><h2 id="events-header"><a name="events" id="events">Upcoming Events</a></h2>
       <?php
       			// display sticky event posts first
       			while ($queryStickyObject->have_posts())	{
       				$queryStickyObject->the_post();
       				if (is_sticky()) { echo 'it really is sticky, dummy!'; }
       				?><h3>first loop</h3><?php
       				display_calendar_event(true);
       			}
   
       			// now display non sticky posts
       			while ($queryNonStickyObject->have_posts())	{
       				$queryNonStickyObject->the_post();
       				if (!(is_sticky())) { echo 'not sticky, dammit!'; }
       				?><h3>second loop</h3><?php
       				display_calendar_event(false);
       			}
       		}
       ```
   
 * If I have one or more sticky posts, it works fine. If all posts are sticky, it
   works fine, so the query constructed with **‘post__not_in’ => get_option(‘sticky_posts’)**
   works fine. However, if I don’t have any sticky posts, the first query generates
   the set of non-sticky posts, and the first loop duly displays them, so I get 
   them twice. I can easily fix this with a fudge, but I’d really like to know what
   is going on!

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

 *  [David Gard](https://wordpress.org/support/users/duck_boy/)
 * (@duck_boy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/problem-with-post_in/#post-1754954)
 * If you have no Sticky Posts then `get_option('sticky_posts')` is returning nothing,
   so you are then adding to the query `'post__in' =>`, which will suspect will 
   pick up every post.
 * Maybe change to –
 *     ```
       if(get_option('sticky_posts') !== null){
           $querySticky = array(
               'category_name' => 'events',
               'posts_per_page' => '20',
               'meta_key' => '_EventStartDate',
               'meta_compare' => '>=',
               'meta_value' => $currentTime,
               'orderby' => 'meta_value',
               'order' => 'ASC',
               'post__in' => get_option('sticky_posts')
           );
           $queryStickyObject = new WP_Query($querySticky);
       }
       ```
   
 *  Thread Starter [madmaia](https://wordpress.org/support/users/madmaia/)
 * (@madmaia)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/problem-with-post_in/#post-1754956)
 * Ah, light dawns! Thank you, hadn’t appreciated that `'post__in' =>` was going
   to behave like that. All sorted without any fudging. Thank you!
 *  [toyNN](https://wordpress.org/support/users/toynn/)
 * (@toynn)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/problem-with-post_in/#post-1755235)
 * I was not understanding this behavior either. I’d been thinking that “post__id
   => get_option(‘sticky_posts’)” would affect the post query to return nothing 
   when there were no sticky posts. But as @duck__boy says – it will actually cause
   the “post__id” option to not affect the rest of the post query.
 * Thanks

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

The topic ‘Problem with post__in’ is closed to new replies.

## Tags

 * [post__in](https://wordpress.org/support/topic-tag/post__in/)
 * [Sticky posts](https://wordpress.org/support/topic-tag/sticky-posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [toyNN](https://wordpress.org/support/users/toynn/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/problem-with-post_in/#post-1755235)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
