WordPress at Your Fingertips

get_previous_post() WP 1.5.0

Retrieves the previous post that is adjacent to the current post. Return post as object.

To get the object of the next post, use get_next_post().

1 time — 0.001871 sec (very slow) | 50000 times — 2.00 sec (fast) | PHP 7.1.5, WP 4.8.1

No Hooks.


null|String|WP_Post. Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.


get_previous_post( $in_same_term, $excluded_terms, $taxonomy );
Whether post should be in a same taxonomy term.
Default: false
Array or comma-separated list of excluded term IDs.
Default: ''
Taxonomy, if $in_same_term is true.
Default: 'category'


#1 Get the previous post and output a link to it (demonstration of how the function works).

$prev_post = get_previous_post();
echo '<a href="' . get_permalink( $prev_post ) . '">'. esc_html($prev_post->post_title) .'</a>';

To get such a link, it is more convenient to use the function: previous_post_link().


Since 1.5.0 Introduced.

Code of get_previous_post() WP 5.8

function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
	return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );

From tag: Relations (of posts terms...)

More from category: Navigation

No comments
    Log In