WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/get adjacent post

Contents

Description

Retrieve adjacent post. Can either be next or previous post.

Usage

<?php get_adjacent_post$in_same_cat$excluded_categories$previous ?>

Parameters

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

Return Values

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

Examples

Notes

  • Uses global: (object) $post
  • Uses global: (object) $wpdb

Filters

$adjacent is either 'previous' or 'next'.

  • "get_{$adjacent}_post_join":
    $join, $in_same_cat, $excluded_categories
  • "get_{$adjacent}_post_where":
    $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories
  • "get_{$adjacent}_post_sort":
    "ORDER BY p.post_date $order LIMIT 1"

Change Log

Since: 2.5.0

Source File

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

Related

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