{"id":3272,"date":"2014-06-03T16:52:41","date_gmt":"2014-06-03T16:52:41","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2014\/06\/03\/get-adjacent-post\/"},"modified":"2014-06-03T16:52:41","modified_gmt":"2014-06-03T16:52:41","slug":"get-adjacent-post","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/","title":{"rendered":"The get_adjacent_post() WordPress function"},"content":{"rendered":"<p>Using the <em>get_adjacent_post()<\/em> 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 <em>get_adjacent_post()<\/em> function.<\/p>\n<h2>Usage<\/h2>\n<p class=\"code_block\">&lt;?php get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ) ?&gt;<\/p>\n<h2>Parameters<\/h2>\n<p>All parameters within the <em>get_adjacent_post()<\/em> 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:<\/p>\n<h3>$in_same_term<\/h3>\n<p>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 <em>false<\/em>.<\/p>\n<h3>$excluded_terms<\/h3>\n<p>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.  <\/p>\n<h3>$previous<\/h3>\n<p>This parameter is simply a boolean that determines if the <em>get_adjacent_post()<\/em> function will display the next post, or previous post.  It accepts boolean operators and defaults to <em>false<\/em>, which would display the previous post unless you define <em>true<\/em> to show the next post.<\/p>\n<h3>$taxonomy<\/h3>\n<p>If you have set <em>$in_same_term<\/em> to true, you can then determine the taxonomy name that the function will be pulling from.  This parameter accepts strings and defaults to <em>category<\/em>.<\/p>\n<h2>Code Examples<\/h2>\n<p>There are various reasons that you may need to use the <em>get_adjacent_post()<\/em> function, but the most common would be to direct users to view your next or previous post.<\/p>\n<h3>Link to the previous post<\/h3>\n<p class=\"code_block\">&lt;?php $prev_post = get_adjacent_post( true, &#8221;, true, &#8216;category_name&#8217; ); ?&gt;<br \/> &lt;?php if ( !empty( $prev_post ) ): ?&gt;<br \/>    &lt;a href=&#8221;&lt;?php echo $prev_post-&gt;guid; ?&gt;&#8221;&gt;&lt;?php echo $prev_post-&gt;post_title; ?&gt;&lt;\/a&gt;<br \/> &lt;?php endif; ?&gt;<\/p>\n<p> <em>Source: <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/get_adjacent_post#Examples\" target=\"_blank\">WordPress Codex<\/a><\/em> <\/p>\n<p>As you can see from the above, the <em>get_adjacent_post()<\/em> function is being called with the <em>$in_same_term<\/em>, <em>$previous<\/em>, and <em>$taxonomy<\/em> parameters defined.  This will allow the previous post from the <em>category_name<\/em> category to be returned within the <em>pres_post<\/em> variable.<\/p>\n<p>Next, a PHP <em>if<\/em> statement is defined to determine if the <em>prev_post<\/em> 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.<\/p>\n<h3>Link to the next post<\/h3>\n<p class=\"code_block\">&lt;?php $next_post = get_adjacent_post( true, &#8221;, false, &#8216;category_name&#8217; ); ?&gt;<br \/> &lt;?php if ( !empty( $prev_post ) ): ?&gt;<br \/>    &lt;a href=&#8221;&lt;?php echo $next_post-&gt;guid; ?&gt;&#8221;&gt;&lt;?php echo $next_post-&gt;post_title; ?&gt;&lt;\/a&gt;<br \/> &lt;?php endif; ?&gt;<\/p>\n<p>This block of code does almost the same as the previous example, but instead the <em>$previous<\/em> parameter is set to <em>false<\/em>.  This will allow the next post to display.<\/p>\n<h3>Displaying an excerpt of the next post<\/h3>\n<p>Another example of using the <em>get_adjacent_post()<\/em> would be to show an excerpt of the next post:<\/p>\n<p class=\"code_block\">&lt;?php <br \/> $next_post = get_adjacent_post( true, &#8221;, false, &#8216;category&#8217; );<br \/> if ( !empty( $next_post ) ):<br \/>     $post_id = $next_post-&gt;ID;<br \/>    $excerpt = get_post( $post_id, &#8221;, &#8221; ); ?&gt;<br \/>     &lt;a href=&#8221;&lt;?php echo $next_post-&gt;guid; ?&gt;&#8221;&gt;&lt;?php echo $excerpt-&gt;post_title; ?&gt;&lt;\/a&gt;<br \/>     &lt;?php echo $excerpt-&gt;post_excerpt; ?&gt;<br \/> &lt;?php endif; ?&gt;<\/p>\n<p>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 <em>get_post()<\/em> function to display the post data that we obtained from the <em>get_adjacent_post()<\/em> function.  Next, we display both a link to the next post, as well as the excerpt from that post below it.<\/p>\n<h3>Additional information that can be pulled from the post<\/h3>\n<p>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 <a href=\"https:\/\/codex.wordpress.org\/Class_Reference\/WP_Post\" target=\"_blank\">WordPress Codex page on WP_Post<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &lt;?php get_adjacent_post(<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\"> Read More ><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4325,56],"tags":[],"class_list":["post-3272","post","type-post","status-publish","format-standard","hentry","category-wordpress-hosting","category-wordpress"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The get_adjacent_post() WordPress function | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"The get_adjacent_post() function in WordPress can be used to get the next or previous post. When used with get_post() additional data can be shown.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The get_adjacent_post() WordPress function | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"The get_adjacent_post() function in WordPress can be used to get the next or previous post. When used with get_post() additional data can be shown.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-03T16:52:41+00:00\" \/>\n<meta name=\"author\" content=\"Jeff Matson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeff Matson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\"},\"author\":{\"name\":\"Jeff Matson\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b\"},\"headline\":\"The get_adjacent_post() WordPress function\",\"datePublished\":\"2014-06-03T16:52:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\"},\"wordCount\":718,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"WordPress Hosting\",\"WordPress Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\",\"name\":\"The get_adjacent_post() WordPress function | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2014-06-03T16:52:41+00:00\",\"description\":\"The get_adjacent_post() function in WordPress can be used to get the next or previous post. When used with get_post() additional data can be shown.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The get_adjacent_post() WordPress function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b\",\"name\":\"Jeff Matson\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/jeffma\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The get_adjacent_post() WordPress function | InMotion Hosting","description":"The get_adjacent_post() function in WordPress can be used to get the next or previous post. When used with get_post() additional data can be shown.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/","og_locale":"en_US","og_type":"article","og_title":"The get_adjacent_post() WordPress function | InMotion Hosting","og_description":"The get_adjacent_post() function in WordPress can be used to get the next or previous post. When used with get_post() additional data can be shown.","og_url":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2014-06-03T16:52:41+00:00","author":"Jeff Matson","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Jeff Matson","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/"},"author":{"name":"Jeff Matson","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b"},"headline":"The get_adjacent_post() WordPress function","datePublished":"2014-06-03T16:52:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/"},"wordCount":718,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["WordPress Hosting","WordPress Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/","url":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/","name":"The get_adjacent_post() WordPress function | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2014-06-03T16:52:41+00:00","description":"The get_adjacent_post() function in WordPress can be used to get the next or previous post. When used with get_post() additional data can be shown.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/get-adjacent-post\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"The get_adjacent_post() WordPress function"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b","name":"Jeff Matson","url":"https:\/\/www.inmotionhosting.com\/support\/author\/jeffma\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=3272"}],"version-history":[{"count":0,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3272\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=3272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=3272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=3272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}