I want to be able to display all posts except a the most recent post which I am doing as follows:-

$last = wp_get_recent_posts( '1');
$last_id = $last['0']['ID'];

echo $last_id; //1477

$args = array('posts_per_page' => 5,
              'post__not_in'   => array($last_id),

$recent_posts = get_posts('numberposts=10&exclude='. $post_id .'"');

if($recent_posts) { ?>
        <?php foreach( $recent_posts as $recent ) { ?>
        <a href="<?php echo get_permalink($recent->ID); ?>"><?php echo $recent->post_title; ?></a>
        <?php } ?>
<?php } ?>

$last_id contains the correct value for the most recent post, but the code is still displaying the ten most recent posts without excluding any.

Any ideas?


Couple of issues here

  • IIRC, exclude should be an array of ID's as exclude get passed to WP_Query as post_not_in

  • You are defining arguments which you don't use. You could have simply passed that to get_posts. Rememeber, all parameters in WP_Query is available as is to get_posts as get_posts uses WP_Query

  • You can simply use 'offset' => 1, to skip the most recent post. You are running total unnecesarry queries here which slows your page down

You can try the following

$args = [
    'posts_per_page' => 5,
    'offset' => 1
$recent_posts = get_posts( $args );

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.