0

How do I avoid Post 11, Post 12" from 4th page of Pagination?

  1. Total number of posts in my blog ( say 15 )
  2. Maximum number of posts, I want to show ( say $max = 10)
  3. Posts per page should be 3 only.

   $max = 10;  
   $items_per_page = 3;
   $paged = ( get_query_var('paged') > 0 ) ? get_query_var('paged') : 1;  
   $query = new WP_Query( array( 'posts_per_page' => $items_per_page  , 'paged' => $paged ) );  

   if( $query->have_posts() ) {  
    while( $query->have_posts() ) {  
     $query->the_post();  
     the_title();  
    }  
   }  

   $total = ceil( $max / $items_per_page );  // 4

   echo paginate_links( array( 'total' => $total ) );  

   wp_reset_postdata();  
   wp_reset_query();

output

Pagination

Page 1      : Post 1, Post 2, Post 3
Page 2      : Post 4, Post 5, Post 6
Page 3      : Post 7, Post 8, Post 9
Page 4      : Post 10, Post 11, Post 12 ==> should show only "Post 10"

Note

From my question, i want to show 10 posts only. paginate_links() generates 4 links.
site.com/page/1/ = shows P1, P2, P3
site.com/page/2/ = shows P4, P5, P6
site.com/page/3/ = shows P7, P8, P9
site.com/page/4/ = shows P10, P11, P12 => Here i need to avoid P11, P12

3
  • Is there no way to solve this issue?
    – user110186
    Commented Jan 4, 2017 at 9:02
  • See my answer and please read the previous duplication also.
    – CodeMascot
    Commented Jan 4, 2017 at 9:06
  • Having public published posts that you don't want to show is a weird situation for me. Without knowing the exact context, Using taxonomies, another custom post type or even private posts seems a more proper way of organization that the one you are trying to implement.
    – cybmeta
    Commented Jan 7, 2017 at 7:34

1 Answer 1

-1

Interesting question. Wp_Query will return all posts that match the criteria. To enhance performance, you can switch off pagination with no_found_rows=true. You want both pagination and limit the amount of results returned. That's not something wp_query can deliver.

So the most obvious though not too elegant solution would be to modify your loop in the same way you modify your pagination to cut off the superfluous results:

while( $query->have_posts() ) {
   if (($wp_query->current_post) =< 10 ) {
      $query->the_post();
      the_title();
      }
  }

An alternative way would be to use wpdb and build your own mysql query. Or use no_found_rows and build your own pagination for the results.

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