{"id":2989919,"date":"2012-08-23T17:46:47","date_gmt":"2012-08-23T17:46:47","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/get-nextprevious-post-id-in-a-particular-loop\/"},"modified":"2016-08-20T15:10:18","modified_gmt":"2016-08-20T15:10:18","slug":"get-nextprevious-post-id-in-a-particular-loop","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/get-nextprevious-post-id-in-a-particular-loop\/","title":{"rendered":"Get next\/previous post id in a particular loop"},"content":{"rendered":"<p>I have a custom loop setup via WP_Query that calls the latest 3 posts from a custom post type called cpt_apps where each post also has a certain custom field associated. I&#8217;m trying to display the post thumbnail of the next and previous posts with each item in the loop.<\/p>\n<p>So for example, I want to show:<\/p>\n<pre><code>Post #1 Title\nPost #1 PostThumbnail\nPost #3 PostThumbnail \/\/ prev post thumb\nPost #2 Post Thumbnail \/\/ next post thumb\n_______\nPost #2 Title\nPost #2 PostThumbnail\nPost #1 PostThumbnail \/\/ prev post thumb\nPost #3 Post Thumbnail \/\/ next post thumb\n_______\nPost #3 Title\nPost #3 PostThumbnail\nPost #2 PostThumbnail \/\/ prev post thumb\nPost #1 Post Thumbnail \/\/ next post thumb\n_______<\/code><\/pre>\n<p>&#8230;for each instance in the loop.<\/p>\n<p>I&#8217;ve tried get_previous_post and get_next_post, but I can&#8217;t seem to limit them to the specific custom field, and also they&#8217;re just showing the current post&#8217;s thumbnail anyway.<\/p>\n<p>Would anyone have any idea as to how this could be done?<\/p>\n<p>Here&#8217;s the code as I have it now: <\/p>\n<pre><code>&lt;?php $my_query = new WP_Query(array ( &#039;post_type&#039; =&gt; &#039;cpt_apps&#039;, &#039;orderby&#039; =&gt; &#039;meta_value&#039;, &#039;meta_key&#039; =&gt; &#039;ll_appfeatured&#039;, &#039;posts_per_page&#039; =&gt; 3) );\n\t\t\t\t  $postnum = 0;\n\t\t\t\t  while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post();\n\t\t\t      $appprice = get_post_meta($post-&gt;ID, &#039;ll_appprice&#039;, true);\n\t\t\t      $appicon = get_post_meta($post-&gt;ID, &#039;ll_appicon&#039;, true);?&gt;\n\n\t\t\t\t  &lt;?php $prevPost = get_previous_post(false); $prevthumbnail = get_the_post_thumbnail($prevPost-&gt;ID, array(445, 322)); ?&gt;\n\t\t\t\t  &lt;?php $nextPost = get_next_post(false); $nextthumbnail = get_the_post_thumbnail($nextPost-&gt;ID, array(445, 322)); ?&gt;\n\n\t\t\t\t&lt;li&gt;&lt;article id=&quot;post-&lt;?php the_ID(); ?&gt;&quot; class=&quot;hentry featured-app-post&quot;&gt;\n\n\t\t\t\t&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;\n\t\t\t\t\t&lt;?php the_post_thumbnail(&#039;apps-large&#039;); ?&gt;\n\n\t\t\t\t\t&lt;div class=&quot;backing&quot;&gt;\n\t\t\t\t\t\t&lt;?php echo $prevthumbnail; ?&gt;\n\t\t\t\t\t\t&lt;?php echo $nextthumbnail; ?&gt;\n\t\t\t\t\t&lt;\/div&gt;\n\n\t\t\t\t\t&lt;div class=&quot;hover-up&quot;&gt;\n\n\t\t\t\t\t\t&lt;h1&gt;&lt;?php if ($appicon != &#039;&#039;) { ?&gt;&lt;img src=&quot;&lt;?php echo $appicon; ?&gt;&quot; class=&quot;app-icon&quot; \/&gt;&lt;?php } ?&gt;&lt;?php the_title(); ?&gt;&lt;\/h1&gt;\n\t\t\t\t\t\t&lt;img src=&quot;\/wp-content\/themes\/dashburst\/img\/apps\/app-average-review.png&quot; \/&gt;&lt;br\/&gt;\n\t\t\t\t\t\t&lt;small class=&quot;p-excerpt&quot;&gt;&lt;?php the_excerpt_rss(); ?&gt;&lt;\/small&gt;\n\t\t\t\t\t\t&lt;a class=&quot;button apps-button&quot; href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;Read More &amp;rarr;&lt;\/a&gt;\n\n\t\t\t\t\t&lt;\/div&gt;\n\n\t\t\t\t&lt;\/a&gt;\n\n\t\t\t\t&lt;\/article&gt;&lt;\/li&gt;\n\n\t\t\t\t&lt;?php endwhile; ?&gt;<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-2989919","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/2989919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/2989919\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=2989919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}