• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Exclude Latest Post from the WordPress Loop in Genesis

Last Updated on January 24, 2020 Favorited: 0 times

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

Related Posts

  • Custom Post List with custom field in Genesis
  • Custom Loop for Post Content in Genesis
  • Customize Genesis Next & Previous Page Link Text
  • Add Single Post Navigation In Genesis
  • Reposition Genesis Archive Pagination

Categories: Free Content, Genesis Tutorials, WordPress Tutorials Tags: custom loop, pagination, wp_query

Reader Interactions

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service