Support » How-To and Troubleshooting » Sticky and random post query

Sticky and random post query

  • buymylife


    Greetings all — hoping to get some help; I reckon I kind of know what the problem is, just can’t seem to solve it on my own.

    I’m trying to query three total posts from the same category (and display some of their custom fields): the first spot must be filled by a sticky post (I only have one); the other two spots need to be randomly populated by other posts in the same category.

    The query I’m using is as follows:

    <?php $sticky=get_option('sticky_posts') ;
    	query_posts('p=' . $sticky[0]);
    	'post__in'  => get_option('sticky_posts'),
    	) . 'cat=3&showposts=2&orderby=rand'); ?>

    So, this seems to mostly do as I want, but the “showposts=2” seems to be my tripping point: most of the time, the sticky post and two random posts will show (for THREE total, exactly as desired). But on the occasional query, only TWO posts will show: the sticky post plus a random. If I raise to showposts=3, sometimes I’ll get 3 total, sometimes I’ll get 4.

    Any idea how to get it so that three posts reliably show, always with the sticky first and the rest random? Am I on the complete wrong track here? Any help GREATLY appreciated!!! Thanks —


Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi


    Forum Moderator

    You’re using query_posts twice? Try grabbing the sticky post using get_posts and reserve query_posts for the 2 random posts with the added parameter caller_get_posts=1 to try and exclude the sticky post from being re-rendered.

    Or investigate Multiple Loops.

    Or rewind the posts…

    $sticky = get_option('sticky_posts');
    $sticky = 'p='.$sticky[0];
    // Do some stuff
    			'post__in'  => get_option('sticky_posts'),
    			'cat' => 3,
    			'showposts' => 2,
    			'orderby' => 'rand'
    // Do some more stuff



    Forum Moderator

    I knew there was something I was forgetting. One of those “on the tip of the brain” moments when all I could think of was resetting the posts cache (which I was sure wasn’t right).

    Cute kitten. Yours?

    Yeah, that’s ollie… 🙂 One of Two…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sticky and random post query’ is closed to new replies.