Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have an example basic wordpress theme template here for you. First, please take a quick look at it to get an idea.

<?php
get_header(); ?>

    <div id="primary">
      <div id="content" role="main">

      <?php if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

          <?php
            /* Include the Post-Format-specific template for the content.
             * If you want to overload this in a child theme then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'content', get_post_format() );
          ?>

        <?php endwhile; ?>

        <?php reddle_content_nav( 'nav-below' ); ?>

      <?php else : ?>

        <article id="post-0" class="post no-results not-found">
          <header class="entry-header">
            <h1 class="entry-title"><?php _e( 'Nothing Found', 'reddle' ); ?></h1>
          </header><!-- .entry-header -->

          <div class="entry-content">
            <p><?php _e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.', 'reddle' ); ?></p>
            <?php get_search_form(); ?>
          </div><!-- .entry-content -->
        </article><!-- #post-0 -->

      <?php endif; ?>

      </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

The simplest way to order posts by the last modified date is by adding this:

<?php query_posts($query_string . '&orderby=modified&order=desc'); ?>

Right above this:

<?php while ( have_posts() ) : the_post(); ?>

But that's not what I want. I am told that it's much better to use get_posts() or WP_Query() (see), than query_posts(). I do not know how to modify the code above as per the suggestions.

If would be very helpful if someone can give a pointer as to how I should use get_posts() or WP_Query() w.r.t the basic template above. Thanks.

share|improve this question
1  
there is nothing wrong with using query_posts() in the main loop. –  Michael Apr 23 '12 at 13:11
5  
one important aspect I never see mentioned about using query posts in the template is that you're discarding the original query and running a new one, so from that perspective, if it's your primary loop, it's more efficient to modify via the pre_get_posts hook before the query is run, as in Brady's answer. –  Milo Apr 23 '12 at 13:33
    
@Milo It would be great if you can add an answer. (Noob here.) –  its_me Apr 23 '12 at 13:42
add comment

2 Answers 2

up vote 5 down vote accepted

You haven't specified where you want this alteration to apply to so I've applied it to just the home page. You may alter to fit where this filter applies to:

<?php
function wpse10691_alter_query( $query )
{
    if ( $query->is_main_query() && ( $query->is_home() || $query->is_search() || $query->is_archive() )  )
    {
        $query->set( 'orderby', 'modified' );
        $query->set( 'order', 'desc' );
    }
}
add_action( 'pre_get_posts', 'wpse10691_alter_query' );
?>

Place code into theme functions.php or package into a plugin.

share|improve this answer
    
The beauty of this function is that I can simply remove && ( $query->is_home() || $query->is_search() || $query->is_archive() ) from it, and all my posts are ordered by Last Modified Date literally everywhere (even in wp-admin). –  its_me Apr 23 '12 at 13:49
add comment

There is nothing wrong with using <?php query_posts($query_string . '&orderby=modified&order=desc'); ?> in this scenario.
WP_query (which I love) Is to be used when you have multiple loops on one page, but you only have one.

get_posts breaks "the loop" and will force you to rewrite your template.

So just use <?php query_posts($query_string . '&orderby=modified&order=desc'); ?>

share|improve this answer
7  
Quick summary of what is wrong with it: 1) performance (extra queries), 2) semantics (not main query), 3) compatibility (not main query), 4) query_posts() is just a terrible function all around that should not be used –  Rarst Apr 23 '12 at 13:56
    
I didn't know you r first point, But I disagree with point 2&3 it is the only (thus main) loop –  janw Apr 23 '12 at 14:25
    
No. It is running completely new loop that is similar to main loop and wrecking actual main loop with it. query_posts() adjusting main loop is just a simplification of process. Inside template is too late to sanely modify main loop because template choice itself depends on main loop. –  Rarst Apr 23 '12 at 14:40
    
So would you recommend wp_query or get_posts? –  janw Apr 23 '12 at 15:53
    
The difference between two is just one abstraction layer. Latter is wrapper for former. –  Rarst Apr 23 '12 at 15:59
add comment

Your Answer

 
discard

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.