trouble with query to fetch next and previous posts

I’m new to wordpress. I’m having trouble with a query I wrote to fetch the previous and next posts. ( I check

$sql = "
SELECT * FROM wp_posts WHERE post_date < ( SELECT post_date FROM wp_posts WHERE ID = {$id} ) LIMIT 1
UNION
SELECT * FROM wp_posts WHERE post_date > ( SELECT post_date FROM wp_posts WHERE ID =  {$id} ) LIMIT 1
";

$wpdb->query($sql);

$arrPosts = $wpdb->last_result;

But I only get one post in $arrPosts. ( I know there are more than two). What’s going on?

Related posts

Leave a Reply

1 comment

  1. Try:

    $arrPosts = $wpdb->get_results($sql);
    

    That said, note that there also is an API to get these two. See in particular:

    get_next_post()
    get_previous_post()