Firts / last post make sense if we have an order to refer to. get_next_post()
function use post date to decide which post is the next. It optionally use also taxonomy, but once you use the function without any argument, only date is relevant, so the first post is the most recent post in same post type.
To get the most recent post you can use a new WP_Query
, get_posts
or wp_get_recent_posts
.
Example using the latter:
<?php
$next_post = get_next_post();
if ( empty( $next_post ) ) {
global $post;
$args = array(
'numberposts' => 1, 'post_type' => $post->post_type, 'post_status' => 'publish'
);
$recent = wp_get_recent_posts( $args, OBJECT );
$next_post = ! empty( $recent ) ? array_shift( $recent ) : FALSE;
}
if ( ! empty( $next_post ) ) :
// your code goes here
endif;
Related Posts:
- Problem excluding category from get_next_post
- Get the ID of the latest post
- Change permalinks for posts but not for custom post types
- Front end post editing using a form
- Set posts of a custom post type to be private by default?
- post_row_actions for custom post type
- Loading post content in FancyBox
- Downsides to not using built-in “Posts” post type?
- get_page_by_title() not returning anything [closed]
- Slug for standard post post_type
- get_queried_object error How to show post count by month in the taxonomy page
- Remove parent from custom post type
- How to create A-Z index listing for custom post types?
- Custom Taxonomy – Tags Metabox is showing instead of Categories
- How to post twitter like updates on wordpress
- How Can I Set the Post Author of a Post I Just Created With PHP?
- WordPress Orderby Numeric Value Not Working
- Deleting Custom Post type data using mySQL command
- Notice: Undefined index: suppress_filters
- How to add post_type=value when editing that post type in the WordPress admin?
- Storing posts from query and accessing later via AJAX call
- If post has custom field then display css-class
- Missing Posts in Custom Taxonomy List
- How can i add thumbnails images to particular post (using code not admin pannel) in wordpress
- Showing posts from different categories and from custom post type
- Showing up my custom post type in primary navigation
- custom post type category count shortcode
- Add post location with mile radius allowing search
- Custom Post Slug same as Parents Category Slug
- How to integrate a form (Ninja Form or Contact Form 7) with Custom Post Types?
- Prioritize posts in query by meta keys?
- Homepage’s content is dependent on the custom field values (set automatically), how do I get homepage to update without manually updating page?
- How to get all tags of a custom post type by id
- How to define which register_post_status goes to which register_post_type?
- Losing Nav Active State in Menu
- Create new custom post and post category of same name
- Load Next WordPress Posts With AJAX?
- Is it a good idea to add a column to the posts table?
- If there is only one post (show elements) else (show other elements)
- Custom post type menu
- Modify Posts from Custom_Post_Type within the plugin
- How do I edit the WordPress post.php file?
- Posts are not rendering perfectly [closed]
- How I can made a custom post type to page templates?
- List all posts in taxonomy term
- list posts of two post types in a single template
- how to redirect to a custom post template
- Post/Custom Post Type URL Access
- New posts label category with “new”
- Front-end Image Upload to Custom Meta Box
- Can’t Get Parent and Child Categories of Custom Taxonomy to Display
- Adding /blog in front of single posts (only)
- Add a form in every post and save data in post meta
- list articles in admin showing nested categories or slugs?
- Search CPT by tag ids
- Post template with breadcrumb needs to show page navigated from
- Define multiple prefixes for custom post type
- Publishing failed. The response is not a valid JSON response
- add all blog posts to folder
- Correct way adding External classes to Custom Post type and output their methods to template
- Show posts from WP Custom Post Type selected from a field in a metabox
- order custom post type posts by custom date
- How to show posts of the same category on a page?
- Assign a Post to a User
- Cannot save pages after migration
- custom post type to post association in wordpress
- Rest Api not working with Iframe in form data
- Need to display author’s email id in the “Edit post” field in wp dashboard. How do I do this?
- Use WP pagination functions on a custom page template
- Adding a nav menu for a custom post type
- How to get all posts related to a taxonomy?
- Post navigation doesn’t show (in custom template, custom post type, custom query)
- call a function when insert and update a custom post type
- Work and Display a Custom Post Type as a normal Post
- Most efficient way of showing children posts?
- Limit or filter edit view in the dashboard to a specific ID only?
- Custom post type with two templates
- One Post with different content, depending on a Page
- Custom Post Type
- Popup panel is only displaying 1 entry ignoring all others
- Custom Post Status & Taxonomies
- Calling the first & last post by category in custom post type
- How to display data with pagaination on backend?
- Edit post meta direct from post.php?
- How to Arrange Support => Thumbnail
- Front-end form submission not creating post
- Programmatically Split A Post Into Multiple Pages
- Post being scheduled that shouldnt be schedulded
- next_posts_link returns same content of 1st page
- Post type cloud like tag cloud?
- Change permalinks for posts but not custom post type?
- Why is Posts page selected when showing single Custom Post Type?
- Get custom post type parent category
- How to delete unnecessary custom post types in the UI
- Navigation won’t update to show full path to single post
- show the most recent date of all posts to display on front end
- Error 403 when posting comments to a custom post type from a different page
- How to make a non-public post on wordpress approval?
- How Do I Add a Custom Post Type URL to Content?
- is therer any wordpress function to retrieve a specific html element from post content