Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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() ) {
    $top->the_post();
    // 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
add comment

Your Answer

 
discard

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.