$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - How to add sort order to incremented and paginated category loop|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - How to add sort order to incremented and paginated category loop

matteradmin8PV0评论

I used this in a loop on a category template to get a numbered lists of posts that continues in the next page:

<?php 
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp; 
   while(have_posts()) : the_post(); $counter++; ?>

     <div class="entry">
         <?php echo $counter; the_title(''); ?>
     </div>

<?php endwhile; ?>

It was suggested by s-ha-dum as an answer to another question on StackExchange.

However, I need this loop to sort posts in asc order, and I am not sure how to do that with all these variables in place. Normally, I would use something like the following before the loop and it would work perfectly:

<?php $sortorder= new WP_Query( 'orderby=date&sort=asc' ); ?>

The end result of the code is this:

<?php 
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp;
   $sortorder= new WP_Query( 'orderby=date&sort=asc' );
   while ( $sortorder->have_posts() ) : $sortorder->the_post(); $counter++ ?>

     <div class="entry">
         <?php echo $counter; the_title(''); ?>
     </div> 

<?php endwhile; ?>

But this is not working as expected. It does sort them in ascending order, but all pages show the same list of titles, with only the number changing. So the same 10 titles are shown in page one, numbered 1-10, then again in page 2, but numbered from 11... and so on.

So please excuse my ignorance and help me understand where I am going wrong and how to fix it.

Thank you.


Update: This is the final working query:

<?php
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp; 
   $query = new WP_Query( array(
       'category_name' => 'research',
       'paged' => $paged, 
       'posts_per_page' => 20, 
       'orderby' => 'date',
       'order' => 'asc',
   ) );

   while ( $query->have_posts() ) : $query->the_post(); $counter++ ?>

      <div class="entry">
          <?php echo $counter; the_title(''); ?>
      </div>

<?php endwhile; ?>

Update #2: Improved answer based on further comments by Jaydip Nimavat.

The loop in the category page template is now:

<?php
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp;

   while ( have_posts() ) : the_post(); $counter++ ?>

      <div class="entry">
          <?php echo $counter; the_title(''); ?>
      </div>

<?php endwhile; ?>

And the functions.php now includes:

// Sort order for research, articles, and literature category pages
function custom_posts_per_page($query) {
    if( !is_admin() && $query->is_category( array('research','articles','literature') )) {
      $qobj = get_queried_object();
      if( isset($qobj->taxonomy) && 'category' == $qobj->taxonomy ) {
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'asc' );
      }
    }
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );

And everything works perfectly. Thank you, Jaydip.

I used this in a loop on a category template to get a numbered lists of posts that continues in the next page:

<?php 
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp; 
   while(have_posts()) : the_post(); $counter++; ?>

     <div class="entry">
         <?php echo $counter; the_title(''); ?>
     </div>

<?php endwhile; ?>

It was suggested by s-ha-dum as an answer to another question on StackExchange.

However, I need this loop to sort posts in asc order, and I am not sure how to do that with all these variables in place. Normally, I would use something like the following before the loop and it would work perfectly:

<?php $sortorder= new WP_Query( 'orderby=date&sort=asc' ); ?>

The end result of the code is this:

<?php 
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp;
   $sortorder= new WP_Query( 'orderby=date&sort=asc' );
   while ( $sortorder->have_posts() ) : $sortorder->the_post(); $counter++ ?>

     <div class="entry">
         <?php echo $counter; the_title(''); ?>
     </div> 

<?php endwhile; ?>

But this is not working as expected. It does sort them in ascending order, but all pages show the same list of titles, with only the number changing. So the same 10 titles are shown in page one, numbered 1-10, then again in page 2, but numbered from 11... and so on.

So please excuse my ignorance and help me understand where I am going wrong and how to fix it.

Thank you.


Update: This is the final working query:

<?php
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp; 
   $query = new WP_Query( array(
       'category_name' => 'research',
       'paged' => $paged, 
       'posts_per_page' => 20, 
       'orderby' => 'date',
       'order' => 'asc',
   ) );

   while ( $query->have_posts() ) : $query->the_post(); $counter++ ?>

      <div class="entry">
          <?php echo $counter; the_title(''); ?>
      </div>

<?php endwhile; ?>

Update #2: Improved answer based on further comments by Jaydip Nimavat.

The loop in the category page template is now:

<?php
   $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $ppp = get_query_var('posts_per_page');
   $counter = ($mypage * $ppp) - $ppp;

   while ( have_posts() ) : the_post(); $counter++ ?>

      <div class="entry">
          <?php echo $counter; the_title(''); ?>
      </div>

<?php endwhile; ?>

And the functions.php now includes:

// Sort order for research, articles, and literature category pages
function custom_posts_per_page($query) {
    if( !is_admin() && $query->is_category( array('research','articles','literature') )) {
      $qobj = get_queried_object();
      if( isset($qobj->taxonomy) && 'category' == $qobj->taxonomy ) {
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'asc' );
      }
    }
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );

And everything works perfectly. Thank you, Jaydip.

Share Improve this question edited Mar 14, 2019 at 8:47 jsmod asked Mar 13, 2019 at 12:15 jsmodjsmod 5013 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Add pagination parameters to WP_Query, Check Here.


Please check your query: $sortorder= new WP_Query( 'orderby=date&sort=asc' );

There is no parameter of posts_per_page and paged, so it will get the same ( 1st ) page result only each time and display the same data.


If you want to manage just some parameters only in the main query, do not use custom query use pre_get_posts hook.

Please check:

<?php
function custom_posts_per_page($query) {
    if( !is_admin() && $query->is_category()) {
      $qobj = get_queried_object();
      if( isset($qobj->taxonomy) && 'category' == $qobj->taxonomy ) {
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'asc' );
      }
    }
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );
Post a comment

comment list (0)

  1. No comments so far