WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to write a script that shows the latest (single) post (in a particular category), however, if the user has chosen to have a post be sticky then the sticky post shows. When the sticky post is removed the latest post is then shown.

The trouble I've been having is trying to is using Wordpress build in methods to do:

Loop through all the 'News' categories
 - if: one of the posts is sticky, echo it out
 - else: echo out the latest post

Hope someone can help guide me.

share|improve this question

What you're looking for is best achieved using a custom query:

$top = new WP_Query( array( 
    'category' => 'YOUR_CATEGORY_SLUG',
    'orderby' => 'post_date',
    'order' => 'DESC',
    'posts_per_page' => 1 
) );

if( $top->have_posts() ) {
    // Do things as if we were in the loop...

By default, sticky posts are brought to the top of any query results.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.