WordPress.org

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get next post

Description

Retrieve next post that is adjacent to current post.

Usage

<?php get_next_post$in_same_term$excluded_terms$taxonomy ?>

Parameters

$in_same_term
(boolean) (optional) Whether post should have the same term.
Default: false
$excluded_terms
(string) (optional) Excluded term IDs.
Default:
$taxonomy
(string) (optional) Taxonomy, if $in_same_term is true. Added in WordPress 3.8.
Default: 'category'

Return Values

  • Post object if successful.
  • Null if global $post is not set.
  • Empty string if no corresponding post exists.

Examples

<?php
$next_post = get_next_post();
if ( is_a( $next_post , 'WP_Post' ) ) { ?>
  <a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo get_the_title( $next_post->ID ); ?></a>
<?php } ?>

Notes

Change Log

Since: 1.5.0

Source File

get_next_post() is located in wp-includes/link-template.php.

Related

get_previous_post(), get_adjacent_post()

See also index of Function Reference and index of Template Tags.