How do you paginate a query grouped by month?

I am currently using David Gard’s solution from the thread below. It works great as it fills the requirements of the OP. Basically grouping the custom posts by month and cap it at 6 months.

Display posts by month

For my purposes, I would like to add pagination to be able to view posts older than 6 months. I have done pagination before but for some reason can’t figure out how to adapt it to his example query (this is more complex than queries I’ve done).

I’ve used the code below to paginate a query based on post count. But the problem I’m trying to solve is how to paginate based on month count (since there could be a different number of posts per month).

Any help is appreciated!

<div class=”pagination”>
<?php
echo paginate_links( array(
‘base’ => str_replace( 999999999, ‘%#%’, esc_url( get_pagenum_link( 999999999 ) ) ),
‘total’ => $query->max_num_pages,
‘current’ => max( 1, get_query_var( ‘paged’ ) ),
‘format’ => ‘?paged=%#%’,
‘show_all’ => false,
‘type’ => ‘plain’,
‘end_size’ => 2,
‘mid_size’ => 1,
‘prev_next’ => true,
‘prev_text’ => sprintf( ‘<i></i> %1$s’, __( ‘Newer Posts’, ‘text-domain’ ) ),
‘next_text’ => sprintf( ‘%1$s <i></i>’, __( ‘Older Posts’, ‘text-domain’ ) ),
‘add_args’ => false,
‘add_fragment’ => ”,
) );
?>

Read more here:: How do you paginate a query grouped by month?

Leave a Reply

Your email address will not be published. Required fields are marked *