How-to show last 5 posts that are published after current post?
Explained on image below:
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityHow-to show last 5 posts that are published after current post?
Explained on image below:
global $wpdb, $post; // Uses current POST here
$IDs = $wpdb->get_col("SELECT `ID` FROM {$wpdb->posts}
WHERE `post_type`='{$post->post_type}' AND `post_status`='publish' AND `ID`<{$post->ID}
ORDER BY `ID` DESC LIMIT 5;");
This function lists the 5 posts with IDs lower then ID 176
that are of post_type='post'
and post_status='publish'
. Change post_type if you use a custom post type to whatever you need. You can also order by post_date_gmt
if you want.
Regards.
<?php
function custom_prev_posts($limit = 5){
global $wpdb, $post;
$html = '';
$prev_posts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_date < '%s' AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date DESC LIMIT $limit", $post->post_date ) );
if($prev_posts){
$html .= '<ul>';
foreach ( $prev_posts as $prev_post ) {
$html .= '<li><a href="' . get_permalink( $prev_post->ID ) . '">' .$prev_post->post_title . '</a></li>';
}
$html .= '</ul>';
}
return $html;
}
echo custom_prev_posts(); ?>
I think using the offset arg will achieve what you are after:
$posts_arr = get_posts('numberposts=5&offset=1');