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.
Related Posts:
- Can the Next/Prev Post links be ordered by menu order or by a meta key?
- Filter next_post_link() and previous_post_link() by meta_key?
- How can I get next/ previous post links to order by a filter (by the last word of the title)?
- Problem excluding category from get_next_post
- specify meta_key / meta_value condition for prev_post_link and next_post_link
- Using previous_post_link and next_post_link to wrap around post sequence
- Working with Next and Prev links in Single Taxonomy Templates
- next_post_link and previous_post_link in custom post type single page, in same non-custom category
- Next and Previous Posts of Same Parent
- Modify previous and next post links to current Authors Other posts
- Why are previous_post_link/next_post_link skipping some posts?
- Add Previous/Next Buttons to CPT Single Pages Only
- Custom Posts and Posts – next_post_link and previous_post_link not working together
- Display next 3 posts based on custom taxonomy
- Include “Scheduled” (“Future”) Posts in WordPress Post Navigation ( previous_post_link, next_post_link ) for a Specific Custom Post Type
- How to get next post link of child custom post type from parent post and get next post link of parent post from the last child post?
- Prev/Next Navigation on Single Post Disappeared for Custom Post Type
- Back, Next, and Previous links for the REFERRING term archive
- get_previous_post_link and get_next_post_link of custom post type that share custom field value
- Previous/Next custom post links within custom taxonomy
- Next/Previous Post Links in single-[custom-post-type].php file
- show previous/next post orderer by title in custom post types
- previous_posts_link returns null when querying custom post type
- Custom Taxonomies: Multiple Hierarchical Permailnks For A Single Post + Prev/Next Links
- Make `previous_post_link()` Function Show The Post After Next i.e. Jump A Post
- next/previous links on custom post single a little screwy
- How to use next_posts_link/previous_posts_link for custom post types paginate?
- CPT : Next or Previous term link when has not adjacent post
- Trying to display next and previous set of posts on separate page (not parent page)
- Previous and Next posts
- Creating a search form for custom fields
- don’t publish custom post type post if a meta data field isn’t valid
- How to order adjacent posts (prev / next) by custom field value?
- Favouriting a Custom Post Type
- How can I generate a RSS feed based on a custom WP_Query?
- How to give position to Submenu under custom post type
- Display posts of custom type in hierarchical order
- Remove the Publish metabox for custom post type [duplicate]
- How to query custom post type by term?
- Post the content of a specific “Custom Post Type” post within a post using a shortcode
- How to get the first term for the current taxonomy?
- Including an External File in a Post (and Using Processed Paths)
- Notice: Undefined property: stdClass::$delete_posts with custom post type
- count_many_users_posts except current logged in user?
- How to search map address in custom post type?
- Ignore sticky posts if post is not in meta query
- Creating Accordians in WordPress Theme
- Custom Search for Drafts in Custom Post Type
- Custom Post Type with has_archive ignores my custom archive and single template
- What’s the difference between same wp functions get_posts(); functions in different form?
- Pagination on a custom post type loop
- Display all post titles of current post type
- Custom post type multiple loop by taxonomy term
- Cannot add category or custom taxonomy from admin. WordPress site is hosted in Windows 16 server via IIS. No XAMPP or WAMP
- How do I set default meta value by post type?
- How to Filter custom post type by taxonomy?
- Add custom post type settings to wordress default posts
- How do I set all of a particular post meta to a value within the custom post type I’m in?
- dropdowboxes with pre-selected custom fields optios that filters the results according seach needs
- Display Repeatable Meta Box Content
- Control content before and after custom post type loop
- Showing custom post type categories in the menu
- Convert comma separated list to serialized array to import as post meta
- WordPress rewrite url
- add pagination to custom category posts
- Improve Custom Post Permalink Structure
- Modifying Dashboard edit.php
- Creating custom post type and taxonomy archives and pretty URLs
- How do I add multiple post types to this query?
- Using custom post type as front home page pagination not working
- Migrate Custom Post Type with Custom Fields data and parent child order
- Archive page for WordPress Custom Post Type doesn’t show pagination from paginate_links()
- How to create review point system for CPTs (many-to-many relationship)
- Search for custom field input of a custom post type in ajax live search
- Register custom_post_type from static function
- WordPress custom post type as homepage with homepage url
- Custom post type single page returns 404
- localize_script or rest api
- Add content to wordpress edit.php page
- Search result based on URL
- Show posts under a selected custom taxonomy term
- Linking Taxonomies
- Custom Post Type with Subfolder in Custom Theme
- Link two different post using there post_id in post meta
- How do I extract the contents of a CPT’s custom field for all posts?
- Get number of posts by a user (draft, pending review & published)
- How can I generate a list of post-type specific categories?
- Publish post and create additional posts with same content
- Excert ( the_excerpt(); ) Not working properly
- Keep same archive.php for post from archive?
- After rewriting post type’s URL, the number of queries has increased dramatically
- Pagination not working on homepage
- How to query custom post then display sections by meta value
- Prevent users from changing post status
- Change next_post and previous_link to navigate throught parent categories only
- A to Z list for custom post types
- What Is My Fault With This WP_QUERY ? [ Pagination Problem ]
- Just the First Metabox what saves the data!
- pre_get_posts causes Custom Posts appear under ‘Pages’ menu
- How to get the proper category in permalink with custom post type assigned to multiple categories