Title: Sorting Multiple query_posts
Last modified: August 20, 2016

---

# Sorting Multiple query_posts

 *  [jtoborek](https://wordpress.org/support/users/jtoborek/)
 * (@jtoborek)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/sorting-multiple-query_posts/)
 * I have multiple query_posts that query the lastest post and that works just fine,
   however now I need to be able to sort those query_posts so that they are arranged
   with the latest query on top. I am very new to WP and PHP so please be aware 
   of that.
 * Here is the code I’m working with:
 *     ```
       <div id="newsRightbox">
             <h2>LATEST NEWS</h2>
   
       <ul>
              <?php
   
       query_posts('cat=5&showposts=1&orderby=date&order=DESC');
       while (have_posts()): the_post()
       ?>
   
       <li>
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo_proquest.jpg" alt="Pro Quest" width="82" height="52" /></a>
   
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><b><?php the_time('F j, Y'); ?></b></a><?php the_title(); ?>
                 <div class="clear"></div>
                  </li>
       <?php break; endwhile; ?>
   
       <? query_posts('cat=6&showposts=1&orderby=date&order=DESC');
       while (have_posts()): the_post()
       ?>
   
       <li>
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo_dialog.jpg" alt="Dialog" width="82" height="52" /></a>
   
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><b><?php the_time('F j, Y'); ?></b></a><?php the_title(); ?>
                 <div class="clear"></div>
                  </li>
       <?php break; endwhile; ?>
   
       <? query_posts('cat=7&showposts=1&orderby=date&order=DESC');
       while (have_posts()): the_post()
       ?>
   
       <li>
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo_bowker.jpg" alt="Bowkar" width="82" height="52" /></a>
   
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><b><?php the_time('F j, Y'); ?></b></a><?php the_title();  ?>
                 <div class="clear"></div>
                  </li>
       <?php break; endwhile; ?>
   
       <? query_posts('cat=8&showposts=1&orderby=date&order=DESC');
       while (have_posts()): the_post()
       ?>
   
       <li>
       	<a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo_navtech.jpg" alt="Navtech" width="82" height="52" /></a>
   
           <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><b><?php the_time('F j, Y'); ?></b></a><?php the_title();  ?>
                     <div class="clear"></div>
                      </li>
       <?php break; endwhile; ?>
   
       <? query_posts('cat=9&showposts=1&orderby=date&order=DESC');
       while (have_posts()): the_post()
       ?>
   
       <li>
       	<a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo_sothebay.jpg" alt="Sotheby's Institute of Art" width="82" height="52" /></a>
   
           <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><b><?php the_time('F j, Y'); ?></b></a><?php the_title();  ?>
                     <div class="clear"></div>
                      </li>
       <?php break; endwhile; ?>
   
       <? query_posts('cat=10&showposts=1&orderby=date&order=DESC');
       while (have_posts()): the_post()
       ?>
   
       <li>
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo_bachtoroch.jpg" alt="Bach to Rock" width="82" height="52" /></a>
   
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><b><?php the_time('F j, Y'); ?></b></a><?php the_title();  ?>
                 <div class="clear"></div>
                  </li>
       <?php break; endwhile; ?>
   
       <? query_posts('cat=14&showposts=1&orderby=date&order=DESC');
       while (have_posts()): the_post()
       ?>
   
       <li>
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="http://www.cig.com/wp-content/uploads/2011/08/cig-small.jpg" alt="CIG" width="82" height="52" /></a>
   
       <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><b><?php the_time('F j, Y'); ?></b></a><?php the_title();  ?>
                 <div class="clear"></div>
                  </li>
       <?php break; endwhile; ?>
   
       </ul>
              <div style="text-align:right; padding: 10px 0 0 0;"><a href="latest-news/">Read more news</a></div>
           </div>
       ```
   

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/sorting-multiple-query_posts/#post-3495553)
 * Here is some sample code that may help.
 * Set up the common query arguments:
 *     ```
       $args = array(
         'posts_per_page' => 1,
         'orderby' => 'date',
         'order' => 'DESC',
       );
       ```
   
 * Then, for each category, use code like this:
 *     ```
       $args['cat'] = 126;
       query_posts( $args );
       if (have_posts()) {
          while (have_posts()) {
             the_post();
             $myposts[] = $post;
          }
       }
       ```
   
 * Finally, sort and output the results:
 *     ```
       usort($myposts, function($a, $b) {
          return strcmp($b->post_date, $a->post_date);
          } );
       foreach ( $myposts as $post ) {
          setup_postdata($post);
          echo '<br />DATE:' . $post->post_date;
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Sorting Multiple query_posts’ is closed to new replies.

## Tags

 * [query_post](https://wordpress.org/support/topic-tag/query_post/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/sorting-multiple-query_posts/#post-3495553)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
