WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

My categories look like this. Each has one or more posts

-Main Category
--Main Category Sub-category 1
--- 1 post here
--- 1 post here
--Main Category Sub-category 2
--- 1 post here
--- 1 post here
--- 1 post here
--Main Category Sub-category 3
--- 1 post here

When viewing Main Category archive page I only want the first post from each of the subcategories. So in my case I would only have 3 posts. Using twentythirteen theme. Don't want to change too much. I hope it can be done with query_posts.

        <?php /* The loop */ ?>
        <?php query_posts($query_string . '&??????'); ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content-category', get_post_format() ); ?>
        <?php endwhile; ?>
share|improve this question
up vote 0 down vote accepted

Don't use query_posts() use the WP_Query class instead... the link to the Codex will also reference all the appropriate query parameters.

The following would query the most recent post in a particular category, you will need to define the $category_id

// The Query
$query = new WP_Query( 'cat' => $category_id, 'posts_per_page' => 1 );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        get_template_part( 'content-category', get_post_format() );

/* Restore original Post Data */
share|improve this answer
ok ... but that only returns one post. I was trying to return one post from each sub-category. – ciprian Aug 26 '13 at 20:25
You didn't say how far along you were. You'll need to use get_categories() and run a foreach() loop. – helgatheviking Aug 26 '13 at 21:07

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.