404 Error in Custom Post Type Pagination – WordPress

Advertisement

It’s been a long time now since my last post and I really missed sharing knowledge, snippets and more, now I’d like to give you one useful snippet and this always happen when adding WordPress pagination specially in custom post type, without lot of words, let’s start.


// custom navigation
$big = 999999999; 		// need an unlikely integer
$total_pages = $the_query->max_num_pages;
if ( $total_pages > 1 )
{
	$current_page = max( 1, get_query_var('paged') );
	$nav_args = array(
		'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
		'format' 	=> '/page/%#%',
		'current' 	=> $current_page,
		'total'		=> $total_pages,
		'show_all'	=> false,
		'prev_text'	=> 'Prev',
		'next_text'	=> 'Next',
		'type' 		=> 'list'
	);
	echo paginate_links( $nav_args );
}

Note

  • The most and only main reason of 404 page not found is the post-type name should not have the same with the current page slug.
    e.g. /course/course/ or it can be mean as /page-slug/post-type
  • After you made changes in your code make sure to update permalinks in WP Dashboard, how? Just hit update or choose default permalink and go back to where it was before.
  • Another reason is the permalink “format”, e.g. you’d coded different permalink structure like ?paged=%#% and in your WP permalink setting is %postname% so in that case they have different formats and that lead to 404 error, how can I correct this? Simple you just need to make both in same format like /page/%#% in code and %postname% in WP dashboard.

Hope that helps

Advertisement