WordPress Numeric Pagination on Archives & Custom Queries

I came across an interesting question (Pagination doesn’t work) that just happened to “catch my eye” on the WordPress Stack Exchange, regarding a Numeric Pagination in WordPress.

In the question asked by @Paul I noticed he made use of the WP_Query class to perform a custom query in order to fetch posts from a Custom Post Type.

That’s totally fine and actually very practical / common when dealing with various WordPress archives.

The problem when you create a custom query is: now you can’t use a lot of the default functionality as you normally would have; You need to make some adjustments, in order to get the WordPress pagination to work properly.

Most of the WordPress functionality has to do with the default query which is performed on every page load. When you create a custom query using WP_Query, you’re not replacing the default query with your custom query. You’re adding a SECOND query (your custom query) to run parallel with the FIRST query (the default query).

Most (if not all of the WordPress functionality) expects the default query, but can be customized in-order to produce the desired result.

It would be best to only have ONE query instead of TWO queries for the requested page, especially if they’re querying the same source.

You can customize the default query several different ways, but the most common way would be to hook into the pre_get_posts action and modify the query before the query is performed. There are also more complex ways of modifying the query, for example, modifying the SQL using the filter hook posts_request.

This can be a tricky task and if not careful can actually cause unexpected behavior in other areas throughout your WordPress website.

This is why WordPress developers often choose to just create a brand new custom query using WP_Query to retrieve exactly what they want, without the worry of interfering with other areas of their WordPress website.

In this article, I’ll be demonstrating how to create a Numeric WordPress Pagination to add to your WordPress archive pages; When the posts have been fetched using a custom query via WP_Query instead of using the default query available by WordPress.

To begin, you’ll need to start by setting up your custom query. Your query might look something along the lines of:

If you want your Numeric Pagination to work with your custom query, then you need to be aware of the current page the request is performed on.

The current page number is actually referred to as paged. You can determine the current page number using some code like this:

Once the current page has been determined, you can setup your custom query to look similar to this:

Once the query has been performed, you then need to check if it’s located any posts in the database which match your criteria:

Finally! Now for the Numbered Pagination! I recommend using a WordPress function called paginate_links() for this part.

I don’t really understand the really unlikely integer part for the base, but that’s what’s demonstrated on the WordPress Codex page linked above.

  • You need to specify the current page (paged) — once again.
  • You need to know the total number of posts (which can be obtained from the $query variable object).
  • You need to specify a return type for this function. (Personally, I like it to be in array format, so that I can customize the pagination however I see fit).

This code needs to be placed within the condition which states that we have posts available. Not inside the loop, but immediately AFTER the loop.

Once the Numeric Pagination has been configured, it’s ready for output (or ready to be displayed). This is where paginated items are returned in array format.

I like to wrap it with a conditional check and then loop through the items performing additional conditional checks along the way.

 

Note: I use Bootstrap for my WordPress themes. If you want your pagination to look awesome, with minimal effort involved. Try out Bootstrap.

To conclude everything mentioned above, here’s a all-in-one copy/paste starter solution for anyone looking to create a Numerical Pagination with a custom posts query using WP_Query.

 

Credits

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] Pagination doesn’t work asked by: @Paul
  2. [WordPress Development] Pagination not working with custom loop asked by: @nurain
  3. [WordPress Development] WordPress custom post type category pagination 404 Error asked by: @21zna9
  4. [WordPress Development] The Pagination doesn’t work asked by: @hazelnut
  5. [WordPress Development] Pagination doesn’t work in custom page template asked by: @dev-jim
  6. [WordPress Development] Custom pagination code is not working asked by: @strange man
  7. [WordPress Development] 404 – Category pagination for custom post type asked by: @waffl
  8. [WordPress Development] Pagination of custom post type leads to 404 error asked by: @Benmay
  9. [WordPress Development] The pagination on index.php doesn’t work on a first page asked by: @Sergei Rodionov
  10. [WordPress Development] Permalinks not working when using a custom query for a custom post type asked by: @Leon
  11. [WordPress Development] Pagination not working it makes no sense asked by: @Grégoire Llorca
  12. [WordPress Development] How do i paginate query_posts with numeric pagination? asked by: @user3277
  13. [WordPress Development] RESOLVE – Numeric pagination custom post type asked by: @skifast
  14. [Stack Overflow] WordPress – Add Numeric Pagination. asked by: @user3559304

Leave a Reply