Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

Explained on image below:

IMAGE FOR EXPLAINE

share|improve this question

3 Answers 3

up vote 0 down vote accepted
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.

share|improve this answer
    
Thanks for your code :)) –  Vital Jan 12 '12 at 16:14
<?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(); ?>
share|improve this answer

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

 $posts_arr = get_posts('numberposts=5&offset=1');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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