$new_query->query('cat=' . $idcat . '&paged=&post_type=post&posts_per_page=2'); global $counter_blog; $counter_blog = 1; while ($new_query->have_posts()) : $new_query->the_post(); $times = get_post_meta(get_the_ID(), 'timenews', true); $times = strtotime($times); if( $fromdate<=$times && $todate >= $times) { get_template_part( 'content-calendar', get_post_format() ); } $counter_blog++; endwhile; previous_posts_link(); echo "\t\t"; next_posts_link(); 

Here there is such code, why shows only 3 pages maximum? I can not understand where to change it, and if you display the 1st record on the page, so are 3 pages of everything filled?

------ update ------

Redid the output like this, now the pagination links display the actual number of pages (before that it showed 3), but all are empty after the third ...

  query_posts(array('cat'=>$idcat,'posts_per_page'=>1, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ) )); if ( have_posts() ) : while ( have_posts() ) : the_post(); get_template_part( 'content-calendar', get_post_format() ); endwhile; endif; echo paginate_links(); 

But the first option is better, since there filtering by date normally works, and in the second it is not normal. :)

  • Print print_r ($ wp_query) and see which SQL query you are creating. Also look at the paginate_links () function; according to the documentation, an array of options should be passed there, including the number of the current page and the total number of pages. - noganno
  • I added more entries and in the second case, the pages are added, the pagination shows the desired number of pages, but the last 2 pages are blank. [max_num_pages] => 7 is all right, as it should be ... - Jbyh

1 answer 1

Never use query_posts() if you are new and do not know what the hook is doing. Just better forget about it, especially without wp_reset_postdata() at the end. Use either get_posts() or new WP_Query .

Further, from a piece of code it is not clear where you get $idcat and why you

global $counter_blog; $counter_blog = 1;

And how do you need to sort the records?

Adequately and correctly rewrite the last UPD to:

 $args = [ 'cat' => $idcat, 'posts_per_page'=>1, 'paged' => ( get_query_var('paged') ) ? get_query_var('paged') : 1, ]; $list = new WP_Query($args); if ( $list->have_posts() ) : while ( $list->have_posts() ) : $list->the_post(); /* Тут выполняем нужные действия с постом или подключаем нужную часть темплейта */ endwhile; echo paginate_links(); endif; 

Additionally, we specify what to do with the pagination function (how to display links or a list of pages), since paginate_links() returns an array.

Explanations for arguments and other things:

$idcat - ID of the category from which you want to display posts.

posts_per_page - the number of posts output per page at a time

paged - definition of the current pagination page

Also, in the first piece of code, when creating a question, there is a sorting by the metafield associated with time, it can be performed immediately in the array of $args parameters and not create additional cycles. In this case, you will immediately have a sorted list of requested posts. In the code that I provided as an example, the default sorting will be provided by the date the post was added.