WP Development - getting next post ID

Hi guys.
I need your help with get_next_post() function. So , I want to get next post’s ID but I’m getting error.
My code :
$next_post = get_next_post();
echo $next_post->ID ;

Warning : Attempt to read property “ID” on string in test.php on line 10

Check the docs, for get_next_post() get_next_post() | Function | WordPress Developer Resources

Null if global $post is not set. Empty string if no corresponding post exists.

For the next post ID you can use:

$next = get_adjacent_post( false, '', false );
echo $next->ID;

You can always check what’s inside your variable using var_dump:

var_dump( $next_post );

1 Like

I tested it , but it says that $next is string too:

<?php $next = get_adjacent_post( false, '', false ); echo $next->ID; ?>

var_dump($next) : string(0)

Interesting. I was using this function on all my themes :cowboy_hat_face: :zipper_mouth_face:

Maybe you are using it outside of the loop?

Oh worked , thank you. Yes it was outside loop :slight_smile: