How to return results from single category in this query?

How can I return the latest post from category 84 in this query?

<?php
            /*$args = array('post_type'=>'post', 'posts_per_page'=>'10', 'post_no_in'=>$do_not_duplicate);*/
            $args = array('post_type'=>'post', 'post_no_in'=>$do_not_duplicate);
            $query = new WP_Query($args);
            query_posts("cat=84&showposts=1&orderby=DESC");
            if($query->have_posts()){
            while(have_posts()) : the_post();   
            /*$do_not_duplicate[] = get_the_ID();*/  ?>
            <div>
               <?php the_post_thumbnail(); ?>
                 <div class="instruction">
                <div class="home_title">MOST READ</div>
                <div class="home_title_desc"><h3><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h3></div>
                 </div>
            </div>

        <?php endwhile; ?>
        <?php } ?>  

I’ve tried ‘cat’ => 84, in the array, but no go. Can you show me how to return results only from category 84?
Thanks

Related posts

2 comments

  1. This code is really messy and actually attemps to run two queries in a row; one of them using query_posts() which is never a good idea (see here for some explanation).

    Here’s the query rewritten for you, getting the latest post from category 84 as requested:

    $args = array(
        'post_type' => 'post',
        'category__in' => 84,
        'order' => 'DESC',
        'posts_per_page' => 1,
    );
    
    $query = new WP_Query($args);
    
    if($query->have_posts()){
        while(have_posts()){
            the_post();
            // do stuff here...
        }
    }
    

    For information on what each of these arguments do, see the WP_Query documentation. In particular, it’s the posts_per_page argument that tells WordPress to only return one post for you.

    I’ve left out 'post_no_in'=>$do_not_duplicate from your original code because I’m not sure what it is doing, but if you want to ask about that feel free, or you can find the appropriate argument in the WP_Query documentation and add it to the $args array above.

Comments are closed.