0

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.

1 Answer 1

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' );
7
  • Can you clarify your answer, please? Because I'm not sure how that works with $counter as well.
    – jsmod
    Mar 13, 2019 at 17:00
  • Check I have edited the answer. Mar 14, 2019 at 6:11
  • Thank you for clarifying. I thought the query was taking my default posts_per_page number from the WordPress settings, so I didn't think to add it. But it is working now, and I have updated my original question to include the final query. However, I also had to add the category_name to the array, despite this being a category template. While the previous version of my query listed the correct category posts without that addition, the final working query gave mixed results and the addition of category_name became a necessity. Do you know if this is expected or am I missing something?
    – jsmod
    Mar 14, 2019 at 7:04
  • 1
    By "gave mixed results" do you mean it gives a result from all the posts ( including custom post type? ) then you need to add post_type parameter. by default, it uses all. please check: codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters Mar 14, 2019 at 7:31
  • 1
    Please check added code for pre_get_posts. In $qobj you have term all details. like term_id, slug, name etc. you can manage that to the if condition. Mar 14, 2019 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.