WordPress.org

Ready to get started?Download WordPress

Forums

Get 5 latest post...excluding today (2 posts)

  1. matthewkris
    Member
    Posted 5 years ago #

    Is there any easy way to accomplish this? The code below is my start but isn't working as I can't seem to grab the post data.

    $catposts = get_posts('category=73'."&numberposts=5");
    foreach($catposts as $single):
         $postday = the_date('d', '', '', FALSE);
        if($postday != $current_day)
    {
    $result .= '<li><a href=';
    $result.=get_permalink($single->ID).'>'.$single->post_title.'</a><br />';
    $result .= $single->the_excerpt."\n".'</li>';
    	}
    endforeach;
  2. MichaelH
    Member
    Posted 5 years ago #

    So you need to display the latest 5 posts in category 73 that are not dated today. Problem is you really don't know how many posts there are today, so you really need to get all posts in that category, then test for the date.

    <?php
    $count = 0;
    $result = '';
    $catposts = get_posts('cat=73&&numberposts=0');
    if ( $catposts ) { //if there are any posts
      foreach($catposts as $single):
        setup_postdata($single);
        $post_date = mysql2date("Ymd", $single->post_date);
        if ( $post_date != date('Ymd') ) {  // if post from today, ignore
          $count++;
          if ( $count < 6 ) {  // only process 5
            $result .= '<li><a href=';
            $result .=get_permalink($single->ID).'>'.$single->post_title.'</a>';
            $result .= $single->the_excerpt."\n".'</li>';
          }
        }
      endforeach;
    echo $result;
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic