73 Responses

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

    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.

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

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

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

    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.

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

    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.

    Reply
  5. Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs « Lorelle on WordPress
  6. Eduardo
    Eduardo April 8, 2009 at 8:42 am |

    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));
    ?>

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

    Damn. Again:


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

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

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

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

    @Nathan,

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

    Reply
  10. Lorelle on WP: Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs | Aslifm Blogu
  11. two7s_clash
    two7s_clash April 15, 2009 at 1:33 pm |

    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?

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

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

    Reply
  13. Wordpress Blog Services - 10 Useful WordPress Loop Hacks
  14. Philip Arthur Moore
    Philip Arthur Moore June 10, 2009 at 5:24 pm |

    @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

    Reply
  15. 10个非常实用的WordPress Loop技巧 | 鹏博客
  16. Sebyrar
    Sebyrar June 15, 2009 at 7:28 am |

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

    Reply
  17. 10 Useful WordPress Loop Hacks - Myfreepedia.com
  18. Michael
    Michael June 29, 2009 at 2:33 pm |

    Is it possible to list sticky posts in the sidebar?

    Reply
  19. 10 Useful WordPress Loop Hacks
    10 Useful WordPress Loop Hacks July 1, 2009 at 11:52 pm |
  20. » Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs
  21. Sarah
    Sarah July 16, 2009 at 4:54 pm |

    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?

    Reply
  22. 40+ Awesome Tutorials and Techniques For WordPress Theme Developers | tripwire magazine
  23. 40+ Awesome Tutorials and Techniques For WordPress Theme Developers | huibit05.com
  24. Casper
    Casper August 22, 2009 at 12:05 am |

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

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

    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.

    Reply
  26. 10 Really Useful WordPress Code Snippets | WPShout.com
  27. 20+ Tutorials For WordPress Theme Developers | oOrch Blog
  28. Tutorial: Latest Sticky Posts | Wordpress Lesezeichen
  29. Tutorial: Letzte Sticky Posts darstellen… | Wordpress Lesezeichen
  30. The Ultimate Wordpress Developer Toolbox | tripwire magazine
  31. How to Display the Latest Sticky Posts in WordPress
  32. Kelvin45
    Kelvin45 October 22, 2009 at 2:47 pm |

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

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

    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?

    Reply
  34. links for 2009-11-25 « Free Open Source Directory
  35. Post 49841 – Digital Tomb
    Post 49841 – Digital Tomb December 2, 2009 at 3:56 pm |
  36. Display sticky post and exclude it from recent posts list in WordPress | WP Garage
  37. 55+ Most Wanted WordPress Tips, Tricks, and Hacks
  38. Kate
    Kate February 17, 2010 at 3:51 am |

    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!

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

    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

    Reply
  40. How To Display a sticky post only # WordPress Tricks & Tips
  41. 55+ Most Wanted WordPress Tips, Tricks, and Hacks | Best Web Magazine
  42. John
    John April 13, 2010 at 3:29 am |

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

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

    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();?>
    Reply
  44. 20+ WordPress Codes You Might Need On Your Next Project | Web Design Philippines | MikeWagan.net
  45. Geek is a Lift-Style. » Blog Archive » 10 Useful WordPress Loop Hacks
  46. Geek is a Lift-Style. »Archive » 10 Useful WordPress Loop Hacks
  47. How to Display the Latest Sticky Posts in WordPress
  48. Nathan
    Nathan July 19, 2010 at 5:58 pm |

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

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

    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?

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

    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?

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

    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' ) ); ?>
    Reply
  52. ryanve
    ryanve January 21, 2011 at 10:10 pm |

    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; ?>
    Reply
  53. 23 Excellent Tutorials For WordPress Theme Developers | WebScriptPlus.com
  54. Mahesh
    Mahesh May 26, 2011 at 3:44 am |

    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.

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

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

    Reply
  56. WordPress: Sticky Posts On Static Front Page | Ipstenu on Tech
  57. 10 Useful WordPress Loop Hacks - Smashing Magazine | Smashing Magazine
  58. Display Latest Sticky Posts in WordPress | Tutorial
  59. Jon
    Jon November 7, 2011 at 1:54 pm |

    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

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

    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;
    Reply
  61. Michael Spree
    Michael Spree January 3, 2012 at 6:14 pm |

    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…

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

    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

    Reply
  63. 10 Useful WordPress Loop Hacks | Template joomla, wordpress premium free , ebook
  64. 55 Complete Outline of WordPress Tips and Tricks | A1onlineE@rning
  65. 55+ Most Wanted WordPress Tips, Tricks, and Hacks | mohsininbd
  66. How to Display the Latest Sticky Posts in WordPress | mohsininbd
  67. Abdessamad Idrissi
    Abdessamad Idrissi March 5, 2013 at 8:14 am |

    ‘caller_get_posts’ deprecated since 3.1 … use ‘ignore_sticky_posts’ instead

    Reply
  68. Courtyard Creative | HOW I REDESIGNED THE COURTYARD CREATIVE WEBSITE
  69. James
    James April 22, 2013 at 7:50 am |

    Is there a hack on how to implement sticky post to custom post types?

    Reply

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.