• Resolved Pete

    (@perthmetro)


    I have a very normal loop inside my index.php I am using for my home page. I also have <?php query_posts( 'order=asc' ); ?> above this loop… like so,

    <?php query_posts( 'order=asc' ); ?>
    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>

    Now whenever I try to click on page 2 it keeps me on page 1Any ay to fix this so I don’t need to change my loop code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Pete

    (@perthmetro)

    I’m not sure if this is the best/correct way but it works now…

    <?php if ( have_posts() ) : ?>
    <?php query_posts($query_string . '&orderby=date&order=asc'); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    Moderator bcworkz

    (@bcworkz)

    query_posts() is not a very good approach under any circumstance these days, I think it’s mainly left in for reverse compatibility. Much better is to use the pre_get_posts action to alter the appropriate query vars without destroying the others, which is what query_posts() does. The examples illustrate typical setups. In your case, once you’ve determined the current query is the one to alter, you would set the query vars with:

    $query->set('orderby', 'date');
    $query->set('order', 'asc');

    Thread Starter Pete

    (@perthmetro)

    Like this?

    function order_home_asc($query) {
      if ($query->is_home() && $query->is_main_query()) {
        $query->set('orderby', 'date');
    	$query->set('order', 'asc');
      }
    }
    add_action('pre_get_posts', 'order_home_asc');

    It sadly seems an awful lot of bother just to arrange a list a of posts by ascending date!

    Thread Starter Pete

    (@perthmetro)

    It seems to work

    Moderator bcworkz

    (@bcworkz)

    That is how it’s done. It is a lot of bother, it happens sometimes with OOP, the price we pay for modularity and flexibility is formalized routines for tasks, be they simple or complex.

    It’s not so apparent, but behind the scenes this is way less bother than calling query_posts(), trust me.

    I simply added:
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
    to my query_posts args. That worked for me.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘query_posts is preventing viewing page2’ is closed to new replies.