Generally speaking, whenever I work with custom post type pagination, it’s done so by writing a custom query using WP_Query and then calling wp_reset_postdata() at the end of The Loop.

Custom Post Type Pagination: WP_Query

I still think this has its place, but there is a simpler solution that you may be able to implement using a specific hook that WordPress provides.

Custom Post Type Pagination

Specifically, I’m talking about the pre_get_posts hook. In the Code Reference, you can see this is defined as:

Fires after the query variable object is created, but before the actual query is run.

And since the query contains a lot of information, we can go ahead and use it to determine if we want to modify the necessary arguments to deal with pagination.

By this, I mean we need to look at the post_type property and the posts_per_page value. To do this, you can implement it in the following way:

View the code on Gist.

The code above isn’t object-oriented (if it were, I’d set up a subscriber and a class responsible for handling the logic in the function), but you can see how to set it up in its most basic form.

So if you’re working with custom post type pagination and don’t want to deal with the various aspects of setting up a custom query, this may solve your problem.


Leave a Reply