get_next_post()
Retrieves the next post (as object) from the specified taxonomy (the default is category).
To get a link to the previous post, use get_previous_post().
Uses: 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
WP_Post|null|String
. 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 the same taxonomy term.
Default: false - $excluded_terms(int[]|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. |
get_next_post() get next post code WP 6.7.2
function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); }