0

How-to show last 5 posts that are published after current post?

Explained on image below:

IMAGE FOR EXPLAINE

0
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.

0
1
<?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(); ?>
0

I think using the offset arg will achieve what you are after:

 $posts_arr = get_posts('numberposts=5&offset=1');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.