80 Responses

  1. Nathan Rice
    Nathan Rice Published |

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

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

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

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

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

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

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

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

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

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

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

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

    Is it possible to list sticky posts in the sidebar?

    Reply
  19. 10 Useful WordPress Loop Hacks
    10 Useful WordPress Loop Hacks at |
  20. » Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs
  21. Sarah
    Sarah Published |

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

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

    Reply
  25. Mobile TTS -
    Mobile TTS - Published |

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

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

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

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

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

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

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

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

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

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

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

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

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

    [...] 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 Published |

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

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

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

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

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

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

    Reply
  70. 55+ Most Wanted WordPress Tips, Tricks, and Hacks « Share IT
  71. Как вывести последние прилепленные записи в WordPress | WPinCode — WordPress без плагинов
  72. Rick Torres
    Rick Torres Published |

    Exactly what I was looking for, works perfect. Thanks Justin!

    Reply
  73. 23+ Excellent Tutorials For WordPress Theme Developers – See more at: http://webdeveloperplus.com/wordpress/23-excellent-tutorials-for-wordpress-theme-developers/#sthash.0JUTYpsR.dpuf | CSE BLOG JUST
  74. 55+ Most Wanted WordPress Tips, Tricks, and Hacks | Readtech's Blog
  75. Show The Latest Sticky Posts In Wordpress - Wordpress Code Snippets
  76. WordPress Local » 10 Useful WordPress Loop Hacks

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.

css.php