Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

$the_id;
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;
}

query_posts($arr);

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

share|improve this question
add comment

1 Answer

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? –  Jackson Apr 12 '12 at 15:21
add comment

Your Answer

 
discard

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.