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 using wp as an engine to put blog features in an existing site. I have a place in the blog page where I load wp-load.php. I want to get the next and previous posts, but when I load a post from from wpdb, both get_next_post() and get_previous_post() both return NULL. What do I need to do to get those functions to return the proper values?

Edit Here's the code:

if ( isset($_GET['article_id']) && is_numeric($_GET['article_id']) ) {

    $id = $_GET['article_id'];
    $objPost = get_post($id);

    new dBug(get_next_post());
    new dBug(get_previous_post());

}

dBug is a pretty-printing class. Currently it gives two NULLs. I tried this:

if ( isset($_GET['article_id']) && is_numeric($_GET['article_id']) ) {

    $id = $_GET['article_id'];
    $objPost = get_post($id);
    setup_postdata($objPost);
    new dBug(get_next_post());
    new dBug(get_previous_post());

} but no luck. Still NULL.

share|improve this question
    
Can you show us the code? –  sorich87 Oct 23 '10 at 17:55
    
Yeah! I don't know why I didn't do it before. –  user1359 Nov 1 '10 at 3:04
add comment

1 Answer

You have to call setup_postdata($post).

get_next_post() and get_previous_post() depend on calls to a number of properties in the global $post variable which is populated by calling setup_postdata($post) or the_post(). If $post is empty it returns null.

share|improve this answer
    
Another thing: the $post variable has to be named $post in order to work. Instead of $objPost = get_post($id); do $post = get_post($id);. (You may want to back up the contents of $post first, so you can restore them afterward.) –  goldenapples Dec 24 '10 at 0:17
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.