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
- next_post_link and previous_post_link on custom post type without taxonomy?
- Get Posts by Category, Tag , and CPT Taxonomy
- How to detect that the save_post hook is calling the callback associated to the current edit post page only
- Custom Post Type – Rewite Archive page
- Custom Post Type – Support “author” only for admins
- register_post_type()’s rewrite slug not working
- Cannot get custom post
- find custom post type post by searching its custom field with my string
- How to access the thank you page from the single post?
- Proceed to the next video of the custom post type but remain on the same post
- WordPress different templates for same type custom post (but different slug)
- wordpress lists similar type of posts in a custom post type
- How do I add custom HTML to the content of an archive page’s posts?
- Conditional tag only for front of custom post type
- FacetWP paging custom wp_query
- why post_class() function apply css classes to all other files?
- Update post if exist from PHP
- Remove taxonomies using register_post_type_args
- Looping Through Categories of a CPT
- Different options per post type in WP_Query
- How to create connection among state, city, store and store number based on URL
- How to add a sub directory to WordPress single posts without affecting other post types?
- Custom post type and standard post not displaying or working
- Change custom taxonomy archive permalink
- What is the diference between Post Metadata and Queried Object
- post_type internal ID?
- Only show posts with a specific term of an associated taxonomy in a custom post type archive
- REST filters only firing when I have a param set
- Is it possible to have “two” sets of tags?
- How do you make a list and list item relation if custom post types?
- How to enable UX builder for custom post types in Flatsome
- search custom post type by custom fields on front end
- Show Taxonomies with admin area for custom post type?
- Custom post type Shortcodes wordpress
- category list with cutom post count
- Empty search input does not return all posts
- How to properly rewrite pagination rules for a CPT to avoid 404 error on /page/2
- After creating Custom post type by user delete old one
- WordPress admin display post type tags as checkbox
- I want to know if i can add two different custom post types to my wordpress site
- SEO Concerns With Rewrite Rules
- Empty taxonomy items for CPT in admin grid
- $post->post_meta not pulling any post in wordpress/php
- How to show my post type TEAM in dynamic way?
- Set parent for custom post type archive rewrite url
- WordPress custom API endpoint – how to make the request more flexible
- Values from meta-box not on $_POST
- How to rewrite the beginning and end of the permalink structure in a custom post type?
- Add to ‘action’ within post.php to allow more actions when editing a Custom Post Type in a plugin
- How to get_term_meta on single custom post?
- custom endpoints on Custom Post Type return 404 page not found
- Search Filter CPT, Custom Loop
- Prevent A Specific Custom Post Type Showing In WP Search Results Page
- ArgumentCountError caused by wp-includes/class-wp-hook.php
- ajax response not recieving
- Reordering admin columns for CPT with custom taxonomy and an added custom field
- Getting post attchment URL to populate a CPT Admin Page Column
- The Difference Between Categories and Tags and Taxonomies and Terms
- How to build a WordPress post review system beside commenting
- Pagination doesnt work in custom post type
- Route subpage to Custom Post Type
- Custom REST endpoint not working to retrieve single posts (“rest_no_route”)
- Archive links for custom post types
- singular_name label in custom post type
- Category Search / Custom Post Type search on my website. Custom Post Types that are “page-like”?
- Many to many to many custom post relationships
- how to create custom post type with dynamic category_name using shortcode
- How to get dropdown instance value in WordPress custom Widget
- page template as a custom post type archive page
- Use core block functionality in a custom block gutenberg