WordPress.org

Ready to get started?Download WordPress

Forums

Sorting Multiple query_posts (2 posts)

  1. jtoborek
    Member
    Posted 1 year ago #

    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>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    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;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic