0

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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