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 want to order my posts by tags with only one WP_Query()

In my WP_Query I would like to display 2 posts with tags portrait

2 posts with tags paysage

2 posts with tags portrait

2 posts with tags paysage

2 posts with tags portrait

2 posts with tags paysage


And I need to order these posts by recents.

What is the query to do that ?


PS : Sorry I cannot use code because I'm with my iPhone.

share|improve this question

Like Eugene mentioned in his answer you need to run a query for each tag. I would create a foreach loop that went through each tag then queried the latest 2 posts from each.

$tags = get_tags();
foreach ( $tags as $tag ) {

    echo '<h3>' .$tag->name. '</h3>';
    $tag_query = new WP_Query( array( 
                              'tag_id' => $tag->term_id,
                              'posts_per_page' => 2,
                              'no_found_rows' => true,
                               ) );
        while ( $tag_query->have_posts() ) : $tag_query->the_post();
        // Do stuff
        endwhile; wp_reset_postdata();
share|improve this answer
Ok, you suggest to create two queries. One for tag portrait and tag paysage. But what about the pagination. The only reason why I want to use only one query is the pagination. With two queries i'll get two paginations on my page. – Steffi Jun 16 '12 at 8:54

It's impossible to do with one query, even from database point of view (without unions).

I would recommend you to use separate queries for each term. Don't invent a wheal, don't over complicate your code.

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.