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="/support/<?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="/support/<?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="/support/<?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

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!