WordPress next_posts_link() Function

Definition
In WordPress, next_posts_link() prints a link to the next page of posts. On a blog or archive page, this is the link to older posts.
It is the classic way to add an "Older posts" button below a list of posts.
Syntax
A few ways to call next_posts_link().
<?php
next_posts_link( 'Older posts »' );
| Call | What it does |
|---|
next_posts_link() | Link with the default text. |
next_posts_link( 'Older posts »' ) | Link with your own text. |
next_posts_link( 'Older', $query->max_num_pages ) | Use it with a custom WP_Query. |
get_next_posts_link() | Return the link instead of printing it. |
It prints the link. If there is no next page, nothing is printed.
Full declaration
next_posts_link( string $label = null, int $max_page = 0 ): void
Parameters
| Parameter | Description |
|---|
$label | string. The link text. Default is Next Page ». |
$max_page | int. The last page number. Use $query->max_num_pages for a custom query. Default is 0, which uses the main query. |
Return Value
| Return Value | Description |
|---|
| void | Nothing is returned. The link is printed, or nothing if there is no next page. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Older posts link
Place it after the Loop on the blog page.
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<div class="pagination">
<?php next_posts_link( 'Older posts »' ); ?>
</div>
<div class="pagination">
<a href="https://example.com/page/2/" >Older posts »</a>
</div>
How It Works
- The Loop prints the posts on this page.
next_posts_link() links to page 2 of the list.- On the last page, no link is printed.
Example 2 — Older and newer links
Use it with previous_posts_link() for two-way paging.
<nav class="pagination">
<?php previous_posts_link( '« Newer posts' ); ?>
<?php next_posts_link( 'Older posts »' ); ?>
</nav>
<nav class="pagination">
<a href="https://example.com/" >« Newer posts</a>
<a href="https://example.com/page/3/" >Older posts »</a>
</nav>
How It Works
- This sample is page 2 of the blog.
previous_posts_link() points back to the newer page.next_posts_link() points on to the older page.
Example 3 — Use with a custom query
Pass max_num_pages when you use your own WP_Query.
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$news = new WP_Query( array(
'posts_per_page' => 5,
'paged' => $paged,
) );
// ... loop here ...
next_posts_link( 'Older news', $news->max_num_pages );
wp_reset_postdata();
<a href="https://example.com/news/page/2/" >Older news</a>
How It Works
paged tells the query which page to load.max_num_pages tells the link how many pages exist.wp_reset_postdata() restores the main query data.
Frequently Asked Questions
It prints a link to the next page of posts in a blog or archive list. That is usually the older posts.
next_posts_link() moves between pages of a list. next_post_link() moves between single posts.
Pass $query->max_num_pages as the second argument.
No. It is for lists such as the blog, archives, and search results.
previous_posts_link() prints the link to the newer page.
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful