Sticky and random post query (5 posts)

  1. buymylife
    Posted 6 years ago #

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


  2. esmi
    Forum Moderator
    Posted 6 years ago #

    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.

  3. Mark / t31os
    Posted 6 years ago #

    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
  4. esmi
    Forum Moderator
    Posted 6 years ago #

    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?

  5. Mark / t31os
    Posted 6 years ago #

    Yeah, that's ollie... :) One of Two...

Topic Closed

This topic has been closed to new replies.

About this Topic