65 Responses

  1. Nathan Rice
    Nathan Rice March 28, 2009 at 1:33 am | | Reply

    Just curious, I haven’t tested this, but using the “limit” parameter on query_posts won’t work? I assume that due to the trouble you went through to write this up, it didn’t work, but I wanted to check anyway.

  2. Nathan Rice
    Nathan Rice March 28, 2009 at 2:19 am | | Reply

    @Justin,
    Yeah, that makes sense.

    BTW, using array_slice on the stickies array will help you reduce some code. Instead of explicitly doing $sticky[0], $sticky[1], etc. you could just do:

    $sticky = array_slice($sticky, 0, 3);

    3 of course would be the limit for the number of stickies you’d want to return with the loop.

    Then you could just do this:

    $sticky = rsort(get_option('sticky_posts'));
    $sticky = array_slice($sticky,0,3);
    query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );

    A little harder to follow, but much more efficient. You could probably even consolodate the first 2 lines.

  3. Thomas Clausen
    Thomas Clausen March 28, 2009 at 2:37 am | | Reply

    I think this would be extremely nice to see in Hybrid.

    When Hybrid News came out I started converting all my featured posts to sticky posts, but alas they just all started showing up in the slider. This could be a fantastic way to re-actualize the sticky post as a featured one.

  4. LGR
    LGR March 28, 2009 at 4:23 pm | | Reply

    If a blog has that many sticky posts perhaps using sticky posts is not the best solution. It might be easier to place the posts into a special category and then query the category to display them.

    Just a thought.

  5. Eduardo
    Eduardo April 8, 2009 at 8:42 am | | Reply

    When you need to show the latest post marked as sticky, you can simply change rsort() for array_reverse(). It will preserve the array, only reversing its order.

    Another thing: for the case you and Nathan solved, we don’t need the rsort() / array_slice() thing. I think this can be done using query_posts:
    $sticky, 'caller_get_posts' => 1, 'orderby' => ID, 'showposts' => 1));
    ?>

  6. Eduardo
    Eduardo April 8, 2009 at 8:46 am | | Reply

    Damn. Again:


    $sticky = get_option( 'sticky_posts' );
    query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1, 'orderby' => ID, 'showposts' => 2 ) );

  7. Nathan Rice
    Nathan Rice April 8, 2009 at 9:37 am | | Reply

    @eduardo,
    If you look at the comments above, Justin said that the showposts parameter didn’t do what it was supposed to do.

  8. Eduardo
    Eduardo April 8, 2009 at 10:18 am | | Reply

    @Nathan,

    Yeah, I know, but it did work for me, really. Worth a shot.

  9. two7s_clash
    two7s_clash April 15, 2009 at 1:33 pm | | Reply

    Hi-

    Awesome tip. I’m trying to display the either the (1) latest sticky post, or, if there aren’t any, just the latest post. But the loop is plopping down the latest sticky post AND the latest post. As noted, the showposts option only seems to limit non-sticky posts. Any suggestions?

  10. Ronny-André Bendiksen
    Ronny-André Bendiksen April 28, 2009 at 6:18 pm | | Reply

    How can I specify which category I want to get the sticky posts from?

  11. Philip Arthur Moore
    Philip Arthur Moore June 10, 2009 at 5:24 pm | | Reply

    @two7s_clash: I was struggling with this also and found this solution in a thread started by Nathan. Basically, put everything between a conditional if tag to check if there are actually any sticky posts available. So, in your case, you would do something along the lines of the following:

    if ( get_option ('sticky_posts') ) { ... // retrieve your one sticky post  }
    else { .. // retrieve your one non-sticky post }

    This is problematic in that two loops will have to be declared (at least I think), but it’s the only way around the problem.

    Cheers,
    Philip

  12. Sebyrar
    Sebyrar June 15, 2009 at 7:28 am | | Reply

    I confirm @Edouardo, showposts parameter work fine. Array solution seems to be cleaner.

  13. Michael
    Michael June 29, 2009 at 2:33 pm | | Reply

    Is it possible to list sticky posts in the sidebar?

  14. 10 Useful WordPress Loop Hacks July 1, 2009 at 11:52 pm |
  15. Sarah
    Sarah July 16, 2009 at 4:54 pm | | Reply

    This is working great thanks!
    I do have a question though and I’m hoping you will respond to this Justin.
    I need to loop 2 times through the same sticky query as I need to create first an unordered list of some custom field value from each stick post and then again to get the title and excerpt out of each. It works fine but the page loads too slow. I’d like to show you if possible by providing a link-would you mind giving me a hand in finding out why the results load so slow?

  16. Casper
    Casper August 22, 2009 at 12:05 am | | Reply

    Thank you SO much – it worked like a charm :D
    (WP 2.8.3)

  17. Mobile TTS -
    Mobile TTS - September 10, 2009 at 2:53 pm | | Reply

    I started converting all my featured posts to sticky posts, but alas they just all started showing up in the slider. This could be a fantastic way to re-actualize the sticky post as a featured one.

  18. Kelvin45
    Kelvin45 October 22, 2009 at 2:47 pm | | Reply

    Here again, a sizeable body of research literature shows that improvement between treatment sessions is the rule rather than the exception, with the majority of clients in successful therapy experiencing significant symptomatic relief earlier versus later in the treatment process. ,

  19. Cracks
    Cracks October 28, 2009 at 7:10 pm | | Reply

    Anyone know how to add something after the sticky posts?

    so if sticky, show after all stickys and before normal posts. if no sticky, show before normal posts?

  20. Post 49841 – Digital Tomb December 2, 2009 at 3:56 pm |
  21. Kate
    Kate February 17, 2010 at 3:51 am | | Reply

    How would you handle two separate sticky post loops? I want to have sticky posts from a specific category in one loop, and then all other sticky posts in another loop. (Two different sections of the home page.) I’ve tried unsuccessfully to do so. Any insights? Thanks!

  22. boddhi
    boddhi March 12, 2010 at 12:49 pm | | Reply

    Hello,
    I’ll put the code in front of my loop as you said but now it only shows my “sticky post”, the other one have dissapeared. Does someone have an idea to solve this ?
    Thanks in advance

  23. John
    John April 13, 2010 at 3:29 am | | Reply

    I’m have been looking for a way to make the second post on my homepage sticky. Does anybody know how to do this?

  24. jason
    jason April 28, 2010 at 11:48 am | | Reply

    I am using this as my way of fetching 2 recent posts from a category, how do i incorporate the sticky feature to this code?

    have_posts()) : $recent->the_post();?>
  25. Nathan
    Nathan July 19, 2010 at 5:58 pm | | Reply

    Great code, thanks for your guys hard work.

    Could you help me. I am using this code and it works. However, under the sticky posts, I want my normal blog posts to show. What would I have to add to the code for that to happen?

    In other words, on my homepage, I want my 2 sticky posts AND then my other blog posts under it.

    Thanks :)

  26. Josh
    Josh July 24, 2010 at 9:47 am | | Reply

    Great little piece of code. My only issue is that when there aren’t any sticky posts, it lists all the posts instead. Any way around that? I’m assuming an if statement but can’t seem to get it to work?

  27. nomadone
    nomadone November 6, 2010 at 7:05 am | | Reply

    Sticky Posts are really complicated, wow.

    Wondering if anyone has managed to get stickies to be listed first in search results if they form part of the posts found by a specific search query?

  28. ryanve
    ryanve January 21, 2011 at 9:53 pm | | Reply

    Is there a way to do this with get_posts() instead of query_posts() ?

    I’m using the code below to display the most recent post in a specific category (id=4) but what I’d like to do is only show the most recent sticky one.

     1, 'offset' => 0, 'category' => 4 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<div class=""> array( 'Thumbnail' ), 'size' => 'thumbnail' ) ); ?>
  29. ryanve
    ryanve January 21, 2011 at 10:10 pm | | Reply

    whoa I figured it out—answered my own question above about how to do it with get_posts(). The code below show the most recent sticky post from category id=4. In this case it shows the thumbnail only but one can edit the html part as needed. I’m using it on a homepage to show the most recent sticky post in 3 categories.

    <?php
    global $post;
    $args = array( 'numberposts' => 1, 'offset' => 0, 'category' => 4, 'include'  => get_option( 'sticky_posts' ) );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<div class="<?php hybrid_entry_class(); ?>"><?php get_the_image( array( 'custom_key' => array( 'Thumbnail' ), 'size' => 'thumbnail' ) ); ?></div><!--.hentry-->
    <?php endforeach; ?>
  30. Mahesh
    Mahesh May 26, 2011 at 3:44 am | | Reply

    Hello, I want to know, How to separate Sticky posts than other posts on Homepage? Like, http://www.labnol.org, it shows the sticky post with thumbnail and other posts without thumbnail on homepage.

  31. Bilal Ahmed
    Bilal Ahmed June 13, 2011 at 3:14 pm | | Reply

    [...] 15. Get The Latest Sticky Posts [...]

  32. Jon
    Jon November 7, 2011 at 1:54 pm | | Reply

    I decided I wanted a bunch of sticky posts instead of a ‘sticky’ category because it seemed intuitively right but then got in a mess because my new stickified posts would appear in an undesired order, i.e. ascending ID:

    ** Even where orderby was set in query_posts() parameters **
    (I don’t think all users experience this; I did.)

    I needed to order my homepage posts by a custom ‘meta’ variable and ended up writing some SQL to do it, so it’s kind of irrelevant that they are sticky, except that the stickiness caused the problem. So this code turns out to be more about working with the wordpress database… anyway here it is:

    get_results($hpsql);
    
    	foreach ($hposts as $post) : setup_postdata($post);
    		$play = $post->play_link;
    		$ord = $post->post_order;
    
    	?>

    Comments welcome! (I’m very amateur at all this.)
    Jon

  33. Jon
    Jon November 7, 2011 at 1:59 pm | | Reply

    Like I said, an amateur. Maybe code will appear this time…

    // select posts that have been manually chosen to appear at top of homepage (HP)
    // - user chooses posts to appear by setting post_order
    
    $hpsql = 'SELECT `P`.* , `M`.`meta_value` AS `post_order`;
    $hpsql .= 'FROM `wp_posts` AS `P`';
    $hpsql .= 'JOIN `wp_postmeta` AS `M` ON ( `M`.`post_id` = `P`.`ID`';
    $hpsql .= 'AND `M`.`meta_key` = "post_order" )';
    $hpsql .= 'WHERE `P`.`post_status` = "publish"';
    $hpsql .= 'ORDER BY `post_order` ASC';
    
    $hposts= $wpdb->get_results($hpsql);
    
    foreach ($hposts as $post) : setup_postdata($post);
    	$ord = $post->post_order;
    
    endforeach;
  34. Michael Spree
    Michael Spree January 3, 2012 at 6:14 pm | | Reply

    Hi, I have a simple solution to this problem. I was trying to get my sticky posts to show on a static page. I couldn’t figure it out, so I made a new category called ‘sticky’, and then i simply called the most recent posts from the category.

    Anything i want sticky, I just list in that category…

  35. Naor
    Naor April 3, 2012 at 6:43 am | | Reply

    Great code, but how can I achieve so there is only one sticky post on homepage (the latest one) and under it the rest of the posts

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.