0

I have a Custom Post Type (project) and a Custom Taxonomy (project_category).

I have created a Page (Projects), which uses a custom template, in which I run a WP_Query to fetch projects and display with a pagination, using the paginate_links function.

To display the custom taxonomy archive I use a template named taxonomy-project_category.blade.php (Blade because I'm using Roots Sage).

The projects and the pagination display correctly. The pagination works well when parsing the projects list. But it displays a 404 page when clicking on the pagination links of a project_category page.

  • /projects/ works, it displays the list of projects, page 1
  • /projects/page/2 works, it displays the list of projects, page 2
  • /project_category/category1 works, it displays the list of projects having the category1, page 1
  • /project_category/category1/page/2 does NOT work, it displays a 404 page.

Here is my call to paginate_links :

paginate_links( [
    'total'    => $query->max_num_pages,
    'current'  => get_query_var( 'paged', 1 ),
    'show_all' => true,
] )

Any idea why the URL /project_category/category1/page/2 is 404 instead of the taxonomy archive paginated page ?

4
  • it's probably because although your custom WP_Query has a second page, the main query that loaded the template that lives at that URL does not. Creating a custom query with WP_Query is not how you change the results, it's a second new additional set of queries. If you want to change which posts the main query displays or change parameters then you should change it using the pre_get_posts filter, and then all of this pagination workaround code and the bugs dissappear.
    – Tom J Nowell
    Commented Sep 9, 2022 at 17:15
  • Note I am assuming that /projects uses a page template, and /project_category uses taxonomy-project_category.php or equivalent theme template. If this is not the case please edit this info into your question so that it's clearer
    – Tom J Nowell
    Commented Sep 9, 2022 at 17:17
  • You're right @TomJNowell, I'm using a page template and tax template. I've edited my question. Commented Sep 9, 2022 at 18:02
  • Ok... the /project_category/category1/page/2 URL is returning a 404 because there are no projects to display on page 2.... I just have 2 projects for this term in DB and I display 10 posts per page... 🤦‍ Commented Sep 9, 2022 at 18:36

0

Your Answer

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