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

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.

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.

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

InMotion Hosting is a leading web hosting company serving customers around the world. We provide reliable, high-performance hosting environments that are affordable and easy to use, but the hallmark of InMotion is excellence in customer service. Our aim is to delight every customer with friendly service that is readily available, expert and effective.
InMotion is a trusted partner for businesses both large and small, with over 10 years in the hosting business and over 100,000 satisfied customers hosting their websites on our servers. We care about our community and the environment, with green data centers and ongoing sustainability programs.

LIMITED TIME ONLY! Get all the amazing speed and performance of our WordPress Hosting Platform, including free SSL and much more. That means you get 2 websites, 80GB of storage, unlimited email accounts and a FREE JetPack Personal License, all for $6.99! https://zcu.io/eCNw