How do I exclude only the latest post from a certain category to display on the front page on WordPress?

I only want the latest post from the category to be excluded from the front page. All others from the same category should be displayed. I can’t seem to figure this one out. Here’s what I got so far but it excludes the entire category from the front page.

function exclude_category2($query) { 
if ( $query->is_home ) { 
$query->set('cat', '-1,-4,-36'); 
} 
return $query; 
} 
add_filter('pre_get_posts', 'exclude_category2'); 

Thanks for the help!

Related posts

Leave a Reply

1 comment

  1. You could use some hook like template_redirect to remove the top element from the global $posts array using array_shift like gok suggests (somewhat, he didn’t really say how to do it). In that case it would look like this

    add_action( 'template_redirect', function() {
        global $posts;
        array_shift( $posts );
    });
    

    But I think a more elegant approach would be this

    add_action( 'loop_start', function( $args ) {
        $args[0]->next_post();
    });
    

    If you put this in functions.php, at the beginning of the WordPress loop, it will automatically go to the next post which means it will skip the first post always.

    If you want to do this only on certain pages, use the relevant template tag like is_home() inside the function. if ( is_home() ) $args[0]->next_post();.

    If you are not using PHP version >= 5.3 you will have to give the function a name, since lower versions of PHP do not support anonymous functions.