I have set up a custom post type called ‘Events’. For reasons that I’m not going to get into, I chose not use archive-events.php
as the archive template for my Events. Instead, I decided to create a custom page template, page-upcoming-events.php
, to use as an archive.
I wrote a custom query in this template to pull in events. I am also limiting posts_per_page to 4. I’ve successfully added pagination links using the previous_posts_link()
and next_posts_link()
functions.
These functions are generating links to the expected urls: /upcoming-events/page/2/, etc.
On page 1, I get the first 4 events and the next events link.
The issue I’m having is that when I go to page 2, I get a 404 error, not events 5-8.
How do I add properly add pagination to a custom page template?
This is what I do for paging. I hope it helps you get on the right track, it may be overkill for what you are trying to do…
First I set $paged:
Then build your $args array, make sure to call in:
Run your query including the needed functions for paging (this is a sample, change what you need to make it yours):
All that’s left is to tell the page where you want the page links:
Again, this may be overkill, but it works well.
Are you sure that you don’t have a page with the same name of the custom post type? If you do, the permalink rewrite rules will get confused and trigger a 404.
A very simple solution is: The page that lists the custom post types is called in plural (eg. products) and the actual post type name is in singular (eg. product). So they don’t conflict and it’s all fine.
Also always make sure to rewrite your .htaccess file when creating a new Custom Post Type.