WordPress.org

Ready to get started?Download WordPress

Forums

Problem with post__in (4 posts)

  1. madmaia
    Member
    Posted 3 years ago #

    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!

  2. duck__boy
    Member
    Posted 3 years ago #

    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);
    }
  3. madmaia
    Member
    Posted 3 years ago #

    Ah, light dawns! Thank you, hadn't appreciated that 'post__in' => was going to behave like that. All sorted without any fudging. Thank you!

  4. toyNN
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic