How can I get next post id after current post id for custom post type

If you are using the code inside a custom post type template, then the get_next_post() will get the next post for you. Then you can use the ->ID and get it’s ID. So:

$next_post = get_next_post();
$next_post_id = $next_post->ID;

If you need their link, simply use next_post_link() or previous_post_link().

These functions use the global post object, so you can manually set the global post data, use them and then reset the global object:

global $post; 
$post = get_post( $ID, OBJECT );
setup_postdata( $post );

// Use get_next_post();

wp_reset_postdata();

Code grabbed from an answer here.

Leave a Comment