From this answer:
Let’s say you want to fetch the ID of the immediately previous post
based on the current post’s ID. This is what you’d do:
get_previous_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
glob al $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the previous post
$previous_post = get_previous_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $previous_post )
return 0;
return $previous_post->ID;
}
You can do a similar thing to fetch the next post’s ID … and you can do this recursively if you need to
get the previous previous post:
$two_posts_ago = get_previous_post_id( get_previous_post_id( $post->ID ) );
Related Posts:
- get_adjacent_post – in same term or category not working
- Next/Previous links doesn’t work in the same category
- Dynamic next and previous post links
- Prev/Next child navigation for current page?
- How can I limit the length of the previous/next posts in my WordPress Theme?
- Problem excluding category from get_next_post
- specify meta_key / meta_value condition for prev_post_link and next_post_link
- connect last post with first post
- Is it possible to nest get_previous_post()?
- previous/next_post_link in the same sub-category?
- next_post_link / previous_post_link not working with WP_Query
- Can’t get the previous and next posts permalink
- How do I display a greyed out next_post_link when there is no next post
- 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?
- How to exclude posts by meta key value in the_post_navigation next prev links?
- Prev/Next Navigation on Single Post Disappeared for Custom Post Type
- Change URL of previous posts and next posts link in WordPress
- How to filter get previous post function by meta value DESC and post date DESC?
- Post in multiple categories do not include in previous / next post if one category in excluded
- Can the Next/Prev Post links be ordered by menu order or by a meta key?
- is it possible to get next_post_link
- Get Previous & Next posts by Post ID
- Filter next_post_link() and previous_post_link() by meta_key?
- wp_nav_menu: check if the list item has children and add a class to anchor link
- How can I add title attributes to next and previous post link functions?
- How to Get Next or Previous Post in a Specific Tag?
- Add custom meta to nav menu items
- Display navigation menu item conditionally based on user capabilities
- How to get next and previous post links, alphabetically by title, across post types, in a shortcode
- get_adjacent_post alternative on a very large db
- Remember the last post I read
- Export navigation menu
- How can I get next/ previous post links to order by a filter (by the last word of the title)?
- next and previous post link ordered alphabetically
- List child pages of a specific page
- How to modify the “after” of last element in wp_nav_menu
- Thumbnail (featured image) of next and previous post is not being properly displayed!
- How do I retrieve menu items from one level only?
- How to use next_post_link and previous_post_link on single posts in search results
- Edit HTML of WordPress navigation bar
- Next/Previous Posts Links: Alphabetically and from Same Category
- Using FlexNav with WordPress [closed]
- How can I get next post id after current post id for custom post type
- Why is previous_post_link and next_post_link working outside of the Loop?
- adding custom fields to next and previous post link
- How to make Next and Previous attached image navigation on the attachment page? [duplicate]
- Customizing Walker_Nav_Menu
- Using previous_post_link and next_post_link to wrap around post sequence
- Get only the path from a function like previous_post_link
- Disable “previous link” in first post and “next link” in last post
- previous / next post with thumbnail
- Break apart wp_list_pages in order to customise it
- next_posts_link not working in custom loop
- Making next_posts_link(); return posts by month
- Show prev and next post links for parent post of current image in attachment page?
- Text symbols in the navigation menu
- WordPress previous_post_link exclude posts with multiple categories
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- Showing Thumbnail from Previous and Next Posts
- Next and prev. post link ONLY within category
- Can this navigation be done easily in WordPress?
- Making some links generated from the wp_nav-menu function unclickeable?
- Working with Next and Prev links in Single Taxonomy Templates
- How to make my pagination loop continuously?
- Trouble with wp_nav_menu, conditional tags and fallback_cb
- Previous/Next Post Within the Category?
- Previous & Next on Index Page broken
- next_post_link and previous_post_link in custom post type single page, in same non-custom category
- Using dividers in menu navigation – where to add code?
- Next and Previous links on a single taxonomy page only link to same term
- Get attachment next and previous by author only
- Twenty Eleven theme- have custom landing page, change nav “home” link
- Exclude Post Format from next_post and prev_post
- Using query_posts inside single.php loop
- previous_post_link() next_post_link() arrow position
- “next_posts_link” and “previous_posts_link” display me NOTHING
- Can I use get_adjacent_post for specific categories?
- Add #anchor to next/previous_post_link
- Next and/or previous post content
- posts_nav_link on single.php
- I need ideas for a complicated menu
- How to get back to same page of post-list – after updating a post
- how to change site tag line for particular pages
- dropdown navigation menu
- Bootstrap Navwalker not displaying
- Previous_post and Next_post – Custom Post Type
- How to add show/hide in navigation setting for pages
- next_post_link for CPT only working for a few posts? weird!
- Exclude Posts From Specific Category from Next and Previous post links
- Home Custom Menu Link not Working
- How Do I Add Next and Previous Links On Single Custom Post Type Only?
- Is it possible to skip certain specified pages when using < prev and next > links?
- Menu with parents, children and or siblings pages display
- Inserting specific images inside navigation menu
- Why do next pages load the same content rather then older articles?
- Navigational drop-down lists in WordPress?
- Select first child/subpage item in wp list pages
- Fallback_cb is messing around with containers
- Only show top-level links in site navigation