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
- Custom pagination for custom post types (by names)
- WordPress dashboard, viewing CPT results in 504
- Merge two custom post types into one admin page?
- Displaying category archive of custom post types
- Tag list & tag index for custom post type
- Numeric pagination custom post type
- Categories under custom post types doesn’t show properly
- get a list of posts from Custom Taxonomy
- Custom post type archive sorted and grouped by date in post meta field
- Comment moderation on custom post types
- Actions that Run on Admin Edit Page Load After Posts Are Queried
- How to set default status to custom post type
- How to retrieve custom field types for all posts in WordPress/PHP?
- Custom post type and taxonomy permalinks – Structure
- Best action hook to create custom post and assign taxonomy terms to it on plugin activation?
- Custom Post Type Post have a Child `Page` Post Type Post
- Add a label to the editor?
- How to get the post type from a category id?
- Custom Post Type won’t Register
- Get image post ID in media upload
- Turn on and off custom post type from admin?
- How to make this menu drop down or like accordion
- Multiple meta_values in the pre_get_posts filter?
- create a new page from a custom post type similar to an authors page
- Several post types on WP Query by tag and taxonomy
- Custom Postype specific changes in admin panel
- Can’t publish custom post type – “You are not allowed to edit this post.”
- Display and register custom categories for custom post types
- Properly flush rewrite rules on plugin activation
- Warning: Invalid argument supplied for foreach() in post.php [closed]
- Adding predefined terms to a taxonomy
- Custom post type – list posts based on meta_key and display them divided by CPT taxonomy category
- Get $post->ID from a URL
- Redirect to archive is single post has a certain term assigned to it?
- Saving fields in a drop-down in WordPress
- Custom taxonomies to define versions of a product
- why is the current page title being output?
- WordPress Custom Post Type – Rewrite Query
- Post URL based on Custom Post Types variables
- Redirect User From Registration Page To CPT (profile page) with custom url
- Admin: Get all user for Custom Role
- WordPress giving a 404 page when passing a year argument different than the current year
- Custom post type permalink structure to include the taxonomy
- Mandatory fields in Custom Post Types [duplicate]
- How to show custom post type on homepage and in categories
- Trying to create a custom post type, post type box doesn’t show up?
- Front End Dropdown Showing All Custom Post Type ‘Agent’
- Create if else for post types in WP_Query ‘post__in’ values
- Return multiples taxonomies with wp_get_object_terms
- How to add CSS class field in Custom Taxonomy?
- How to create a product details slider on post?
- I’m trying to create an if statement that will only display my button link for tickets if its under ‘admission-event’… what is wrong with my code?
- Looping to organize and display custom posts by category using PHP and WordPress
- My post loop needs to have 8 different post templates
- How to enable post types on Page Attributes?
- Change custom post type permalink according to category
- Pagination URL in Custom Content Type
- Displaying custom taxonomy related to each post type
- In WordPress do I create tables for my plugin or do I create custom post types for a Student and Classes Registration system?
- Related posts by taxonomy [duplicate]
- Multiple Queries and Loops within CPT Archive
- Displaying information from Custom Fields in template with Types?
- Problem with Custom Post Types, Taxonomy, permalink, Archive
- Add text to metabox input text field from Thickbox
- pagination/prev and next page links not showing
- Get custom post fields and display them
- Check for custom field value in different post type than current one and do something
- Custom post type, when type non existing post name, it query the nearest possible post name by default. How can I disabled this behavior
- How can I show template dropdown in custom post type?
- What would be the optimal CPT / Taxonomy design for the following entities structure