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:
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 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:
<-- Works
<-- Works
<-- 404
Reading Settings: Blog pages show at most 1 posts
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/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/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/projects/page/2
<-- Works
http://domain/projects/category/category_name
<-- Works
http://domain/projects/category/category_name/page/2
<-- 404
Reading Settings: Blog pages show at most 1 posts
1 Answer
Reset to default -2Can you try this?
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
archive-project
is not the template WordPress will use for a taxonomy term archive, see the Template Hierarchy for taxonomies. – Milo Commented Nov 2, 2013 at 18:16pre_get_posts
to alter main queries before they're run- pagination problems solved. – Milo Commented Nov 3, 2013 at 15:11pre_get_posts
to modify the main query, your code will be simpler, smaller, and faster! See the comment from @Milo – Tom J Nowell ♦ Commented May 10, 2016 at 19:55