WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sort posts by last updated/modified (5 posts)

  1. stevechatterton
    Member
    Posted 4 years ago #

    I'm looking for a simple way to display the recent posts on my home page not in the order that they were created but in the order they were last modified. The site is about book reviews and we link to new reviews as they come up elsewhere & we want to show our freshest content right up front.

    Any ideas? I've been trying to figure it out for about a week now and I keep coming up empty handed.

  2. MichaelH
    Member
    Posted 4 years ago #

    query_posts offers the orderby=modified argument you could use.

  3. stevechatterton
    Member
    Posted 4 years ago #

    That would be fantastic, MichaelH, except for one thing:

    The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page.

    This has to be used for two lists side by side on one page. See the 2 lists under the New York Times Best Sellers box on this page for clarification:

    http://www.thelitreview.com

    If I can do it without a plugin I'd be very thankful, as I get a few memory problems working in the admin module as it is & don't want to aggravate the situation.

  4. MichaelH
    Member
    Posted 4 years ago #

    List will display 5 posts in last modified order:

    <?php
    $args=array(
      'orderby'=> 'modified',
      'order' => 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 5,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  5. stevechatterton
    Member
    Posted 4 years ago #

    You're a brilliant man, MiachaelH. Oddly enough, it's almost like you were looking at my source code. Your suggested modification worked itself in almost seamlessly.

    I did change the order to DESC, though. I got a lot of really old posts with ASC. Other than that, this is exactly what I've been looking for. Cheers!

Topic Closed

This topic has been closed to new replies.

About this Topic