1

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

11
  • try with re-flush permalinks settings, hope will solve your problem
    – Anjum
    Nov 2, 2013 at 17:53
  • why are you running a custom query instead of using the default main query?
    – Milo
    Nov 2, 2013 at 18:11
  • also - archive-project is not the template WordPress will use for a taxonomy term archive, see the Template Hierarchy for taxonomies.
    – Milo
    Nov 2, 2013 at 18:16
  • 1
    @waffl - your question says "category archives for a custom post type", and your example URL that doesn't work appears to be for a taxonomy term. anyway, don't run a custom query, get rid of all that code, run the normal loop, use pre_get_posts to alter main queries before they're run- pagination problems solved.
    – Milo
    Nov 3, 2013 at 15:11
  • 1
    Why are you creating a brand new query? Use pre_get_posts to modify the main query, your code will be simpler, smaller, and faster! See the comment from @Milo
    – Tom J Nowell
    May 10, 2016 at 19:55

1 Answer 1

-2

Can you try this?

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
2
  • Also Can you change this line $current_page = max(1, get_query_var('paged')); by $current_page = max(1, get_query_var('page')); Jul 31, 2014 at 17:51
  • 2
    For starters, you can edit your answers. That being said, an answer might not need to be a thorough solution - proofs of concept are valid as well - but they should be more than a hint of what an OP could try. Either this is what he or she should do to remedy the issue, or it isn't an answer... Jul 31, 2014 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.