This tutorial provides the steps to exclude 1,2, 3 or any number of latest published posts on the Post page (Blog) in Genesis.
It will also work with other WordPress Themes.
The simplest solution is adding offset which will remove the number of posts added or using posts_per_page in a custom function hook to the pre_get_posts action.
However, most of the time this will break the pagination if default WordPress Pagination is not added in the theme.
WordPress Codex offers an easy solution to fix this using found_posts filter.
Let’s first see the basic code to offset the posts.
function dy_exclude_latest_post( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'offset', '1' ); } } add_action( 'pre_get_posts', 'dy_exclude_latest_post', 1 );
Now there is a chance that Your Pagination shows the duplicate Post list.
Now we are going to use offset and manual Pagination.
STEP 1
Let’s create the custom query in child-theme/functions.php
add_action('pre_get_posts', 'myprefix_query_offset', 1 ); function myprefix_query_offset(&$query) { //Before anything else, make sure this is the right query... if ( ! $query->is_home() ) { return; } //First, define your desired offset... $offset = 3; //Next, determine how many posts per page you want (we'll use WordPress's settings) $ppp = get_option('posts_per_page'); //Next, detect and handle pagination... if ( $query->is_paged ) { //Manually determine page query offset (offset + current page (minus one) x posts per page) $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp ); //Apply adjust page offset $query->set('offset', $page_offset ); } else { //This is the first page. Just use the offset... $query->set('offset',$offset); } }
Change 3 with the number of the post need to be excluded. Conditions can be used with multiple loops like
! is_admin() && $query->is_main_query()
! ( $query->is_home() || is_main_query() )
! $query->is_main_query()
with is_home()
add_action('pre_get_posts', 'myprefix_query_offset', 1); function myprefix_query_offset($query){ //return if this is not post page if (! is_home() ) { return; } //Before anything else, make sure this is the right query... if (! $query->is_main_query() ) { return; } //First, define your desired offset... $offset = 3; //Next, determine how many posts per page you want (we'll use WordPress's settings) $ppp = get_option('posts_per_page'); //Next, detect and handle pagination... if ($query->is_paged ) { //Manually determine page query offset (offset + current page (minus one) x posts per page) $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp ); //Apply adjust page offset $query->set('offset', $page_offset); } else { //This is the first page. Just use the offset... $query->set('offset', $offset); } }
STEP 2
Fix the Pagination using found_posts() filter hook in child-theme/functions.php
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 ); function myprefix_adjust_offset_pagination($found_posts, $query) { //Define our offset again... $offset = 3; //Ensure we're modifying the right query object... if ( $query->is_home() ) { //Reduce WordPress's found_posts count by the offset... return $found_posts - $offset; } return $found_posts; }
Change 3 as per STEP 1.
Done.
References:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
https://wordpress.stackexchange.com/questions/155758/have-different-number-of-posts-on-first-page/155782