I am having trouble getting the pagination to work on category archives for a custom post type.
The number of pages/links is accurate, but clicking on the link 404s. (Only in the category archive, pagination is working elsewhere in the site)
The URL is formatted like so: http://domain.com/projects/category/category_name
The Permalink structure is: /%category%/%postname
My loop is as follows (in the archive-project.php
template for the custom post type project
)
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$queryArgs = array(
'post_type' => 'project',
'posts_per_page' => 10,
'paged' => $paged
);
if (isset($project_cat)) {
$queryArgs = array_merge($queryArgs, array(
'category_name' => $project_cat
));
}
$projectQuery = new WP_Query($queryArgs);
The second page is generated as http://domain.com/projects/category/category_name/page/2
which returns a 404.
The code for my pagination is as follows (Maybe it’s not the best route, but it works on every other part of the site, after much headache as well)
global $projectQuery;
$total_pages = $projectQuery->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'show_all' => true,
'format' => '/page/%#%',
'prev_next' => false,
'current' => $current_page,
'total' => $total_pages,
));
}
As mentioned, the page links appear to be accurate, but visiting the Page 2
link gives a 404. Any ideas would be greatly appreciated, I have spent hours on pagination alone, it is a surprisingly complex issue with WordPress.
I’ve managed to narrow it down that the paged variable is not working properly here, (get_query_var('paged')) ? get_query_var('paged') : 1;
is always returning 0
although maybe it is due to the formatting of the link.
Additional quick notes:
http://domain.com/projects/page/2
<– Works
http://domain.com/projects/category/category_name
<– Works
http://domain.com/projects/category/category_name/page/2
<– 404
Reading Settings: Blog pages show at most 1 posts
Can you try this?