Support » Fixing WordPress » Get 5 latest post…excluding today

  • matthewkris

    (@matthewkris)


    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;
Viewing 1 replies (of 1 total)
  • MichaelH

    (@michaelh)

    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;
    }
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Get 5 latest post…excluding today’ is closed to new replies.