• Hi,

    I’m trying to remove the latest post form my post loop that spits out posts and categorises them into year. I figured that adding ‘offset => 1 to the array would do the trick but that’s not working. Any ideas what I’m doing wrong?

    This is my function:

    function posts_by_year() {
      // array to use for results
      $years = array();
    
      // get posts from WP
      $posts = get_posts(array(
        'numberposts' => -1,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
        'post_status' => 'publish',
        'offset' => 1
      ));
    
      // loop through posts, populating $years arrays
      foreach($posts as $post) {
        $years[date('Y', strtotime($post->post_date))][] = $post;
      }
    
      // reverse sort by year
      krsort($years);
    
      return $years;
    }

    And my loop:

    <?php foreach(posts_by_year() as $year => $posts) : ?>
    	<h2><?php echo $year; ?></h2>
    	<ul>
    <?php foreach($posts as $post) : setup_postdata($post); ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>
    <?php endforeach; ?>
  • The topic ‘Removing latest post from loop (offset not working)’ is closed to new replies.