get_next_post() WP 1.5.0
Retrieves the next post that is adjacent to the current post. Return post object.
To get a link to the previous post, use get_previous_post().
Works based on: get_adjacent_post()
1 time — 0.00286 sec (very slow) | 50000 times — 1.85 sec (fast) | PHP 7.1.5, WP 4.8.1
No Hooks.
Return
null|String|WP_Post. Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
Usage
get_next_post( $in_same_term, $excluded_terms, $taxonomy );
- $in_same_term(true/false)
- Whether post should be in a same taxonomy term.
Default: false - $excluded_terms(array|string)
- Array or comma-separated list of excluded term IDs.
Default: '' - $taxonomy(string)
- Taxonomy, if $in_same_term is true.
Default: 'category'
Examples
#1 Display the next post
Next post in relation to the previous one will be shown, if such a post exists:
<?php $next_post = get_next_post(); if( ! empty($next_post) ){ ?> <a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo $next_post->post_title; ?></a> <?php } ?>
Changelog
Since 1.5.0 | Introduced. |
Code of get_next_post() get next post WP 5.6.2
function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
}