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
- Get Previous & Next posts by Post ID
- Filter next_post_link() and previous_post_link() by meta_key?
- How can I add title attributes to next and previous post link functions?
- Export navigation menu
- How can I get next/ previous post links to order by a filter (by the last word of the title)?
- Using FlexNav with WordPress [closed]
- Making next_posts_link(); return posts by month
- WordPress previous_post_link exclude posts with multiple categories
- Showing Thumbnail from Previous and Next Posts
- Trouble with wp_nav_menu, conditional tags and fallback_cb
- Previous/Next Post Within the Category?
- Previous & Next on Index Page broken
- Next and Previous links on a single taxonomy page only link to same term
- Twenty Eleven theme- have custom landing page, change nav “home” link
- “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
- posts_nav_link on single.php
- 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
- How Do I Add Next and Previous Links On Single Custom Post Type Only?
- Inserting specific images inside navigation menu
- Select first child/subpage item in wp list pages
- Fallback_cb is messing around with containers
- WordPress – Responsive navbar doesnt work
- Exclude post format from navigation
- How to add swipe action to wordpress pages
- Can I use multiple nav_walkers?
- (Bluehost) navigating to the dashboard via mydomain.com/wp-admin/ results in directory listing
- Using characters to separate menus in navigation bar
- get_next_post_link() and get_previous_post_link() return wrong posts
- previous_posts_link and next_posts_link, how to return the next/previous post’s title as the url
- WordPress next/prev links Issue
- Displaying the author of next and previous posts in pagination?
- Single.php Category Post Count
- Why are previous_post_link/next_post_link skipping some posts?
- Switch form Next/Previous to post title
- add a menu item to navigation menu to link to external url
- Using categories on navigation bar instead of pages
- WordPress post pagination continuous [duplicate]
- Get previous/next post from (specific) category
- WordPress post next/prev not working
- Tweak next_post_link for it to include thumbnail
- Ecommerce Single Product Next Product Link
- Include scheduled ( future ) posts in WordPress post navigation ( previous_post_link, next_post_link )
- Display all posts thumbnails when viewing a single post
- Is wp3.8 / twenty fourteen’s Sticky Nav & Responsive – core or theme based
- Add ID to target navigation link
- Possible to hide Page if no posts from a CPT?
- Listing Sub-Pages & Sub-Sub-Pages
- navigation among single posts
- only show children of a nav menu
- Depth problem using wp_list_pages for subnavigation
- Next/Previous Product with custom order by price & products inside the product category [closed]
- next/previous post links for specific category when post belongs to multiple categories
- Navigating WordPress Articles From Parent Category?
- is_tax() conditional tag not working
- Show category in Previous/Next links
- WP Bootstrap Nav (not JS, not missing ID)
- Using previous_post_link next_post_link but with a filter on a custom field
- 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
- Current Post Parent Category & Child Category Links
- I need to customize wordpress “previous” “next” links
- Conditionally (cpt) filter previous and next_post_link
- Navigation template
- Connect custom rewrite rules and query variables to wordpress navigation
- WordPress not showing pagination links
- Add default wp_nav_menu classes to my custom links
- Adding nav menu name from plugin options dynamically
- Custom navigation bar via wp_list_pages is broken for blog roll
- All of the “more results” and navigation links lead to a 404 error
- determine if specific page is in list?
- Navigation dropdown issue, need it to work on hover
- Retrieving next_post_link() and previous_post_link() in functions.php
- how to implement next/prev within category archive?
- I would like to hide menu on desktop but still have it visible on small screen
- How to add nofollow to the_post_navigation?
- Apply walker_nav_menu_start_el but only to one menu