WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination: last page missing (2 posts)

  1. omeld
    Member
    Posted 1 year ago #

    Hello.

    Following the example in the Codex, I've set up pagination for use with my custom post type/custom taxonomy archive (all books in the given series). However, it behaves rather strangely: the last page (correctly calculated and linked) gives me a 404.

    This is my code for pagination:

    echo paginate_links(
    	array(
    		'base' => get_pagenum_link() . '%_%',
    		'format' => '?paged=%#%',
    		'current' => max(1, get_query_var('paged')),
    		'total' => $myBookTitles->max_num_pages
    	)
    );

    And this it my query:

    $args = array(
    		'posts_per_page' => 9,
    		'paged' => get_query_var('paged'),
    		'post_status' => 'publish',
    		'post_type' => 'book',
    		'meta_key' => 'datepublished',
    		'orderby' => 'meta_value_num',
    		'order' => 'DESC',
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'series',
    				'field' => 'slug',
    				'terms' => $wp_query->query_vars['term']
    			)
    		)
    	)
    );
    
    $myBookTitles = new WP_Query($args);

    This works for pages 1 to n - 1. The resulting permalink structure is /series/name/n.

    If, on the other hand, I change the query and the pagination to use the query_var 'page' instead of 'paged' (and thus bypass rewriting), it works as expected.

    Any thoughts and pointers would me much appreciated.

    Thank you.

  2. omeld
    Member
    Posted 1 year ago #

    I've finally found the solution! The problem is described here and is related to posts_per_page not being passed on correctly.

    My solution was to add a pre_get_posts filter:

    add_action('pre_get_posts', 'my_correct_posts_per_page', 1);
    function my_correct_posts_per_page($query) {
    	if (is_tax('series')) {
    		$query->set('posts_per_page', 9);
    		return;
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic