WordPress.org

Codex

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

Function Reference/get previous post

Description

Retrieve previous post that is adjacent to current post.

Usage

<?php get_previous_post$in_same_cat$excluded_categories ?>

Parameters

$in_same_cat
(boolean) (optional) Whether post should be in same category.
Default: false
$excluded_categories
(string) (optional) Excluded categories IDs.
Default: ''

Return Values

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

Examples

<?php
$prev_post = get_previous_post();
if (!empty( $prev_post )): ?>
  <a href="<?php echo $prev_post->guid ?>"><?php echo $prev_post->post_title ?></a>
<?php endif ?>

Notes

Change Log

Since: 1.5.0

Source File

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

Related

get_next_post(), get_adjacent_post(), previous_post_link()

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