• Hi,

    I’m trying to get the first x number of stickies and I can’t find a way to make it work.

    First off, the most commonly listed method doesn’t work:

    $sticky=get_option('sticky_posts') ;
    query_posts('p=' . $sticky[0]);

    As far as I can tell, the array returned by get_option doesn’t come back in the correct order, thus not returning the first sticky when you use it to call query posts with the 0 item.

    Secondly, numberposts with get_posts and showposts with query_posts does not appear to work for this, either.

    Thanks.

Viewing 14 replies - 1 through 14 (of 14 total)
  • query_posts(array('post__in'=>get_option('sticky_posts')));
    Thread Starter rwc

    (@rwc)

    greenshady,

    Yes, that returns all sticky posts, but I want it to return, say, the latest sticky post, or the latest three sticky posts. Say you have 100 stickies set (yes, it’s possible, depending on how you implement them), you don’t want to be returning that huge result set every time when you don’t need to. Anybody?

    Thread Starter rwc

    (@rwc)

    As stated, this feature works when you don’t provide the sticky filter, but **doesn’t work** when dealing with sticky posts.

    Feel free to try it — if you can get it to work and care to copy in the code for me, maybe I’ve made an error, but I’ve been over it dozens of times and it’s not working for me.

    Thread Starter rwc

    (@rwc)

    Er, was that last response supposed to be blank?

    Just to clarify, I meant by my last post that the showposts qualifier doesn’t work when it comes to stickies — at least from my experimentations. Works great with a general query, though.

    It doesn’t seem to work when I specifically add sticky posts either. There seems to be an issue with post__in and get_option( 'sticky_posts' ).

    I’ve set up an example with 3 sticky posts. I’m trying to only load 2.

    One would think this code would work for loading the first 2 sticky posts, but it loads all 3:

    $sticky = get_option( 'sticky_posts' );
    query_posts( array( 'post__in' => array( $sticky[0], $sticky[1] ) ) );

    I’ve tried this with showposts, caller_get_posts, several other arguments, and just mixing and matching everything I can think of. I don’t believe showposts is correct because we’re specifying the posts to show.

    This seems so simple, but it’s become a bit frustrating. post__in is supposed to only load the posts specified.

    Thread Starter rwc

    (@rwc)

    Good to know I’m not the only one. Maybe someone from the WordPress team could provide a response to this?

    Thread Starter rwc

    (@rwc)

    greenshady, in response to this code:

    $sticky = get_option( 'sticky_posts' );
    query_posts( array( 'post__in' => array( $sticky[0], $sticky[1] ) ) );

    The other issue with this is that you’re assuming that $sticky is getting the posts in the proper order, which it doesn’t, at least not always. So even if post__in was working properly (which you say it isn’t), the code still wouldn’t guarantee the most recent two sticky posts.

    I could’ve sworn I tried this before and it didn’t work, but now it does:

    <?php
    	$sticky = get_option( 'sticky_posts' );
    	query_posts( array( 'post__in' => array( $sticky[0], $sticky[1] ), 'caller_get_posts' => 1 ) );
    ?>

    But, as you’ve mentioned, the problem still persists with getting the latest stickies. The array of stickies are not set according to date, so no luck there.

    This might just need an entirely custom function written. I’m interested in seeing how to make this happen now, so I’ll see what I can come up.

    Maybe someone from the WordPress team could provide a response to this?

    As a rule, those guys don’t post here in the support forums.

    OK. I got it. We just need to sort the array of sticky posts in order of ID (highest ID first). So, we use the rsort() PHP function to do it.

    <?php
    	/* Get all sticky posts */
    	$sticky = get_option( 'sticky_posts' );
    
    	/* Sort the stickies with the newest ones at the top */
    	rsort( $sticky );
    
    	/* Query 2 newest stickies */
    	query_posts( array( 'post__in' => array( $sticky[0], $sticky[1] ), 'caller_get_posts' => 1 ) );
    ?>

    The only thing I don’t really like about this method is that you have to type in $sticky[0], $sticky[1], $sticky[2], and so on instead of setting showposts. But, it’s what we have.

    And, even better code (easier way to define the number of sticky posts):

    <?php
    	/* Get all sticky posts */
    	$sticky = get_option( 'sticky_posts' );
    
    	/* Sort the stickies with the newest ones at the top */
    	rsort( $sticky );
    
    	/* Get the 2 newest stickies (change 2 for a different number) */
    	$sticky = array_slice( $sticky, 0, 2 );
    
    	/* Query sticky posts */
    	query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
    ?>

    I wrote a blog post about this and got a little extra help with it.

    Thread Starter rwc

    (@rwc)

    The only thing about that is, you’re assuming the highest post ID is the most recent sticky set. When in fact, if you have a post today set to a sticky, and afterward, you go back three months in your archive and set another sticky, it’s the three-month-old post that should be the most recent sticky. If I’m not mistaken, your method does not accommodate this part of sticky-post functionality.

    No, I’m not assuming that. I specifically added that in because you said:

    …the code still wouldn’t guarantee the most recent two sticky posts.

    I’m guessing you meant that you wanted the posts that were last made sticky. It’s a bit confusing when we use the term “recent”.

    Changing this line will allow you to get the two posts that were most recently made sticky:

    rsort( $sticky );

    Change it to:

    $sticky = array_reverse( $sticky );
Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Get first x stickies’ is closed to new replies.