Pagination is essential for WordPress sites with multiple pages of content. Without pagination, archive pages would load thousands of posts at once, destroying performance and overwhelming users. WordPress provides built-in pagination functions, but they require proper setup to work correctly with custom queries.
This guide covers how to implement pagination in WordPress, from basic post pagination to advanced AJAX-powered infinite scroll.
Default Post Pagination
WordPress handles pagination for the main query automatically. The Settings > Reading page lets you set the number of posts per page. The posts_nav_link() and paginate_links() functions generate pagination links for the main query. Use the_posts_pagination() for numbered pagination links with current page highlighting.
For post navigation within a single post — previous and next post links — use previous_post_link() and next_post_link(). These link to adjacent posts in chronological order.
Custom WP_Query Pagination
When you create a custom WP_Query, pagination doesn’t work automatically. Set the ‘paged’ parameter to the current page number, and use max_num_pages from the query object to determine the total number of pages. Pass the query’s max_num_pages to paginate_links() for correct paginated output.
Get the current page number with get_query_var(‘paged’) or max(1, get_query_var(‘page’)). The first parameter works for archive pages, the second for single post pagination within a single page of content.
Comment Pagination
Comments can be paginated with the Settings > Discussion page. Enable comment pagination and set the number of comments per page. WordPress handles comment pagination automatically with paginate_comments_links(). Use the comments_nav_link() function to display previous/next comment navigation.
Comment pagination uses get_query_var(‘cpage’) for the current page. The default order is oldest comments first, but you can change this in Discussion settings to show newest comments first.
AJAX Infinite Scroll
Infinite scroll loads more posts when the user scrolls to the bottom of the page. Implement it with the WordPress REST API or admin-ajax.php. When the user reaches the bottom, your JavaScript sends a request for the next page of posts, appends them to the existing list, and updates the current page counter.
Use the Intersection Observer API to detect when the user reaches the bottom of the page — it’s more efficient than scroll event listeners. Load the next page via the REST API using /wp/v2/posts?page=2&per_page=10. Render the returned posts on the client side with JavaScript.
SEO Considerations
Paginated content creates duplicate content issues if not handled correctly. Use rel=”next” and rel=”prev” on paginated pages to indicate their relationship to search engines. Most SEO plugins add these automatically. Use canonical URLs pointing to the first page to consolidate ranking signals.
Infinite scroll can cause SEO issues because search engines may not crawl content loaded via AJAX. Use progressive enhancement — provide standard pagination links that work without JavaScript, then enhance with infinite scroll. This ensures search engines can access all content while providing a smooth user experience.
The Bottom Line
Pagination is essential for WordPress sites with multiple pages of content. Use the built-in pagination functions for the main query, handle pagination explicitly in custom queries, paginate comments for long discussion threads, and consider AJAX infinite scroll for a modern user experience. Always ensure paginated content is accessible to search engines through proper rel attributes and progressive enhancement.
