40 responses to “Get the latest sticky posts in WordPress”

  1. Nathan Rice

    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

    @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

    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

    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. Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs « Lorelle on WordPress

    [...] Get the latest sticky posts in WordPress [...]

  6. Eduardo

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

  7. Eduardo

    Damn. Again:


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

  8. Nathan Rice

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

  9. Eduardo

    @Nathan,

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

  10. Lorelle on WP: Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs | Aslifm Blogu

    [...] Get the latest sticky posts in WordPress [...]

  11. two7s_clash

    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?

  12. Ronny-André Bendiksen

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

  13. Wordpress Blog Services - 10 Useful WordPress Loop Hacks

    [...] Get the latest sticky posts in WordPress [...]

  14. Philip Arthur Moore

    @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

  15. 10个非常实用的WordPress Loop技巧 | 鹏博客

    [...] Get the latest sticky posts in WordPress [...]

  16. Sebyrar

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

  17. 10 Useful WordPress Loop Hacks - Myfreepedia.com

    [...] Get the latest sticky posts in WordPress [...]

  18. Michael

    Is it possible to list sticky posts in the sidebar?

  19. 10 Useful WordPress Loop Hacks

    [...] Get the latest sticky posts in WordPress [...]

  20. » Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs

    [...] Get the latest sticky posts in WordPress [...]

  21. Sarah

    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?

  22. 40+ Awesome Tutorials and Techniques For WordPress Theme Developers | tripwire magazine

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

  23. 40+ Awesome Tutorials and Techniques For WordPress Theme Developers | huibit05.com

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

  24. Casper

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

  25. Mobile TTS -

    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.

  26. 10 Really Useful WordPress Code Snippets | WPShout.com

    [...] Justin Tadlock link [...]

  27. 20+ Tutorials For WordPress Theme Developers | oOrch Blog

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

  28. Tutorial: Latest Sticky Posts | Wordpress Lesezeichen
  29. Tutorial: Letzte Sticky Posts darstellen… | Wordpress Lesezeichen

    [...] Letzte Sticky Posts darstellen http://justintadlock.com/archives/2009/03/28/get-the-latest-sticky-posts-in-wordpress Tweet This!Share this on TechnoratiSubmit this to NetvibesAdd this to Mister WongMark this on [...]

  30. The Ultimate Wordpress Developer Toolbox | tripwire magazine

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

  31. How to Display the Latest Sticky Posts in WordPress

    [...] credit to this code goes to Justin Tadlock and partially to Nathan Rice for coming up with the array slice [...]

  32. Kelvin45

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

  33. Cracks

    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?

  34. links for 2009-11-25 « Free Open Source Directory

    [...] Get the latest sticky posts in WordPress I came across an interesting question the other day. A user wanted to know how to query a specific number of sticky posts for the front page. Generally, I just point people to the Definitive Sticky Posts Guide for WordPress, but this question was not covered. (tags: Get the latest sticky posts in WordPress) Possibly related posts: (automatically generated)101 Techniques for a Powerful CMS using WordPressFor lost Second Lifers…My another blog [...]

  35. Post 49841 – Digital Tomb

    [...] Get the latest sticky posts in WordPress [...]

  36. Display sticky post and exclude it from recent posts list in WordPress | WP Garage

    [...] best solution I found for how to display a WordPress sticky post was a comment by Eduardo on Justin Tadlock’s post Get the Latest Sticky Post. He gave the following code: [...]

  37. 55+ Most Wanted WordPress Tips, Tricks, and Hacks

    [...] credit to this code goes to Justin Tadlock and partially to Nathan Rice for coming up with the array slice [...]

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 (e.g., 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.