Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Next/Previous Post ID using Current Post ID in Wordpress

I want to write a custom next/prev function to dynamically display post information in a fancybox pop-up. So I need to use PHP to get the next and previous Post ID based on whatever Post is currently showing. I know what the current post ID is and I can send that to a function ok, but I can't figure out how to use that ID to obtain the adjacent IDs.

Edit:Here is my code so far (which is not working)

<?php
require_once("../../../wp-blog-header.php");

if (isset($_POST['data'])){
    $post_id = $_POST['data'];
}else{
    $post_id = "";
}
$wp_query->is_single = true;
$this_post = get_post($post_id);
$in_same_cat = false;
$excluded_categories = '';
$previous = false;
$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);


$post_id = $next_post->id;
$title = $next_post->post_title;

$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );

//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.

echo json_encode($dataset);


?>
like image 874
Jarrod Ballou Avatar asked Jun 12 '11 20:06

Jarrod Ballou


People also ask

How do I get the next post link in WordPress?

next_post_link( string $format = '%link &raquo;', string $link = '%title', bool $in_same_term = false, int[]|string $excluded_terms = '', string $taxonomy = 'category' ) Displays the next post link that is adjacent to the current post.

How do I find the previous post title in WordPress?

previous_post_link( string $format = '&laquo; %link', string $link = '%title', bool $in_same_term = false, int[]|string $excluded_terms = string $taxonomy = 'category' ) Displays the previous post link that is adjacent to the current post.

How can I get post ID from post slug?

If you want to get post id by slug in WordPress, you can do so using a function that passes the slug as a parameter and returns the post ID or Page ID. This may sound like a complicated WordPress function but it is very straightforward and easy to implement in your theme or a custom plugin.


2 Answers

get_adjacent_post() uses the global $post as its reference point, so you'll want to replace this:

$this_post = get_post($post_id);

with this:

global $post;
$post = get_post($post_id);

WordPress also provides get_next_post() and get_previous_post(), which you can use here instead of using get_adjacent_post() with all of those arguments. Here's the final product:

<?php
require_once("../../../wp-blog-header.php");

if (isset($_POST['data'])){
    $post_id = $_POST['data'];
}else{
    $post_id = "";
}
$wp_query->is_single = true;

global $post;
$post = get_post($post_id);

$previous_post = get_previous_post();
$next_post = get_next_post();

$post_id = $next_post->id;
$title = $next_post->post_title;

$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );

//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.

echo json_encode($dataset);

?>

I'm not sure what keys you'd like to use for the IDs and titles of the previous and next posts in the $dataset array, so I'll leave that as is for now.

like image 148
Tom Avatar answered Oct 13 '22 04:10

Tom


To get this to work I had to change $next_post->id; to $next_post->ID;, i.e. capitalise ID.

I used it in Wordpress on index.php like this – I inserted this at the top of the loop:

<div class="work-post" id="<?php the_ID(); ?>">

then within the loop I use this: `

        if (isset($_POST['data'])){
            $post_id = $_POST['data'];
        }else{
            $post_id = "";
        }
        $wp_query->is_single = true;

        global $post;
        $post = get_post($post_id);

        $next_post = get_next_post();
        $previous_post = get_previous_post();

        ?>

        <!-- call only if a value exists -->
            <?php if($next_post) : ?>
                <a href="#<?php echo $next_post->ID;?>" class="anchorLink"><div>PREV.</div></a>
            <?php endif; ?>
        <!-- call only if a value exists -->

        <!-- call only if a value exists -->
            <?php if($previous_post) : ?>
                <a href="#<?php echo $previous_post->ID;?>" class="anchorLink"><div>NEXT</div></a>
            <?php endif; ?>
        <!-- call only if a value exists -->`
like image 21
Arvid Eriksson Avatar answered Oct 13 '22 04:10

Arvid Eriksson