WordPress.org

Support

Support » How-To and Troubleshooting » Display Last x Posts in Ascending Order?

Display Last x Posts in Ascending Order?

  • CaptainBlue

    @captainblue

    I’m trying to display the last 20 posts on a particular page and <?php query_posts(‘cat=3&posts_per_page=20’); ?> does this. But how can I display them in reverse order, so the oldest is at the bottom?

    If I use <?php query_posts(‘cat=3&posts_per_page=20&order=ASC’); ?> this reverses the order but displays the first 20 posts, not the most recent 20.

    Help!

Viewing 1 replies (of 1 total)
  • MichaelH

    @michaelh

    I think this works:

    <?php
    $myposts = get_posts('cat=3&showposts=20');
    if ($myposts) {
    $uc=array();
      foreach ($myposts as $mypost) {
        $uc[]=$mypost->ID;
        }
      }
        $args=array(
          'post__in' => $uc,
          'orderby' => 'post_date',
          'order' => 'ASC',
          'posts_per_page' => 20,
          'caller_get_posts' => 1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'Category 3, lastest posts, sorted oldest to newest';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><small><?php the_time('m.d.y') ?> </small><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    [edit]

Viewing 1 replies (of 1 total)
  • The topic ‘Display Last x Posts in Ascending Order?’ is closed to new replies.
Skip to toolbar