Trying to exclude first 5 posts from the first page on the homepage

offset overrides pagination, because when you get down to the query level, it’s paginated via offset.

You can still use offset though, you just have to do some math to multiply your desired offset by the current page number (note that this calculation works because posts per page and offset are both 5, you may have to use the posts_per_page value in your calculation if the two are different):

function my_function_for_excluding_posts( $query ) {
    if ($query->is_home() && $query->is_main_query()) {
        $offset = 5;
        $paged = 0 == $query->get( 'paged' ) ? 1 : $query->get( 'paged' );
        $query->set( 'offset', $paged * $offset );
    }
}
add_action( 'pre_get_posts', 'my_function_for_excluding_posts' );

EDIT- filtering found_posts so number of pages is correct.

function myprefix_adjust_offset_pagination($found_posts, $query) {
    if ( $query->is_home() && $query->is_main_query() ) {
        return $found_posts - 5;
    }
    return $found_posts;
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );

Leave a Comment