WordPress next_posts_link() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints a link

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().

WordPress
<?php
next_posts_link( 'Older posts &raquo;' );
CallWhat it does
next_posts_link()Link with the default text.
next_posts_link( 'Older posts &raquo;' )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
WordPress
next_posts_link( string $label = null, int $max_page = 0 ): void

Parameters

ParameterDescription
$labelstring. The link text. Default is Next Page &raquo;.
$max_pageint. The last page number. Use $query->max_num_pages for a custom query. Default is 0, which uses the main query.

Return Value

Return ValueDescription
voidNothing 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.

WordPress
<?php while ( have_posts() ) : the_post(); ?>
  <h2><?php the_title(); ?></h2>
<?php endwhile; ?>

<div class="pagination">
  <?php next_posts_link( 'Older posts &raquo;' ); ?>
</div>

How It Works

  1. The Loop prints the posts on this page.
  2. next_posts_link() links to page 2 of the list.
  3. On the last page, no link is printed.

Example 2 — Older and newer links

Use it with previous_posts_link() for two-way paging.

WordPress
<nav class="pagination">
  <?php previous_posts_link( '&laquo; Newer posts' ); ?>
  <?php next_posts_link( 'Older posts &raquo;' ); ?>
</nav>

How It Works

  1. This sample is page 2 of the blog.
  2. previous_posts_link() points back to the newer page.
  3. 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.

WordPress
<?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();

How It Works

  1. paged tells the query which page to load.
  2. max_num_pages tells the link how many pages exist.
  3. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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