3

Hello I am using WordPress custom post type. In this I need to get the post id of the next post which will be present in the custom post type.

The issue I am facing is that this post id will be dynamic So please let me know if there is a function which can help me to get the next post id of custom post type in WordPress.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy