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 NULL
s. 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
.
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)
orthe_post()
. If $post is empty it returns null.