posts_per_page offet code example
Example 1: posts_per_page offet
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {
if ( ! $query->is_home() ) {
return;
}
$offset = 10;
$ppp = get_option('posts_per_page');
if ( $query->is_paged ) {
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('offset', $page_offset );
}
else {
$query->set('offset',$offset);
}
}
Example 2: posts_per_page offet
function wpsites_exclude_latest_post( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'offset', '1' );
}
}
add_action( 'pre_get_posts', 'wpsites_exclude_latest_post', 1 );
Example 3: posts_per_page offet
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
$offset = 10;
if ( $query->is_home() ) {
return $found_posts - $offset;
}
return $found_posts;
}