The get_adjacent_post() WordPress function

Using the get_adjacent_post() function in WordPress will allow you to display information on the next and previous posts within WordPress. For example, if you wanted to put links to the bottom of your posts that users could click on to visit the next or previous posts, you would use the get_adjacent_post() function.

Usage

<?php get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ) ?>

Parameters

All parameters within the get_adjacent_post() function are optional, although you will want to define some parameters to ensure that you get the most accurate data possible. The following are parameters that can be defined:

$in_same_term

This parameter determines if the next or previous post needs to share the same taxonomy term as the current post. It accepts boolean operators and defaults to false.

$excluded_terms

This parameter can be an array or comma-separated list of excluded term IDs. This is similar to the $in_same_terms parameter, but instead of including, it simply excludes the taxonomy terms that you define.

$previous

This parameter is simply a boolean that determines if the get_adjacent_post() function will display the next post, or previous post. It accepts boolean operators and defaults to false, which would display the previous post unless you define true to show the next post.

$taxonomy

If you have set $in_same_term to true, you can then determine the taxonomy name that the function will be pulling from. This parameter accepts strings and defaults to category.

Code Examples

There are various reasons that you may need to use the get_adjacent_post() function, but the most common would be to direct users to view your next or previous post.

Link to the previous post

<?php $prev_post = get_adjacent_post( true, ”, true, ‘category_name’ ); ?>
<?php if ( !empty( $prev_post ) ): ?>
<a href=”<?php echo $prev_post->guid; ?>”><?php echo $prev_post->post_title; ?></a>
<?php endif; ?>

Source: WordPress Codex

As you can see from the above, the get_adjacent_post() function is being called with the $in_same_term, $previous, and $taxonomy parameters defined. This will allow the previous post from the category_name category to be returned within the pres_post variable.

Next, a PHP if statement is defined to determine if the prev_post variable is empty. If it is not empty, it will place the GUID of the post within the URL, and the post title as the anchor text within a link.

Link to the next post

<?php $next_post = get_adjacent_post( true, ”, false, ‘category_name’ ); ?>
<?php if ( !empty( $prev_post ) ): ?>
<a href=”<?php echo $next_post->guid; ?>”><?php echo $next_post->post_title; ?></a>
<?php endif; ?>

This block of code does almost the same as the previous example, but instead the $previous parameter is set to false. This will allow the next post to display.

Displaying an excerpt of the next post

Another example of using the get_adjacent_post() would be to show an excerpt of the next post:

<?php
$next_post = get_adjacent_post( true, ”, false, ‘category’ );
if ( !empty( $next_post ) ):
$post_id = $next_post->ID;
$excerpt = get_post( $post_id, ”, ” ); ?>
<a href=”<?php echo $next_post->guid; ?>”><?php echo $excerpt->post_title; ?></a>
<?php echo $excerpt->post_excerpt; ?>
<?php endif; ?>

As you can see from the above example, we are getting the next adjacent post just like the previous examples, but then we are using the get_post() function to display the post data that we obtained from the get_adjacent_post() function. Next, we display both a link to the next post, as well as the excerpt from that post below it.

Additional information that can be pulled from the post

In addition to the post title and GUID that we got from the post, additional information may be gathered from the post as well. For more information on additional information stored within the array, take a look at the WordPress Codex page on WP_Post

0 thoughts on “The get_adjacent_post() WordPress function

  1. this is brilliant, the only example of this that i’ve got to work, however there’s one small mistake:

    “link to next post” should be <?php if ( !empty( $next_post ) ): ?> not <?php if ( !empty( $prev_post ) ): ?>

    – otherwise it doesnt work when you get to the first post – well thats what happened to me anyway:-)

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X