WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm grabbing a post based on a querystring parameter. How can I get the neighboring posts for a previous and next button?

This will get tricky when you're on the first/last items.

Here is how I'm getting the target post:

if($_GET['the_id']) $the_id = $_GET['the_id'];
$arr = array('post_type' => 'my_cpt', 'showposts' => 1);

if($the_id >= 0) {
    $arr['p'] = $the_id;


*note that if there was now querystring param, I'm just loading from the most recent post of that type.

share|improve this question
up vote 1 down vote accepted

use get_adjacent_post(). if nothing is returned for next/prev, get the first/last.

Edit- just noticed your custom post type, you'll also have to filter get_previous_post_where and get_next_post_where to pick up your cpt.

share|improve this answer
This is my first time using something like this. I've looked for examples and none of them are working so far. Will you kindly show me how this should look? – Jacksonkr Apr 12 '12 at 15:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.