exclude latest post in WordPress og

Do you want the most recent post to be excluded from the WordPress post loop?

This allows you to determine whether or not your most recent blog post should appear on your home page or other sites that feature your blog articles.

Advertisements

We’ll show you how to remove the most recent post from the WordPress post loop in this article.

WordPress Post Loop

Why Isn’t the Most Recent Post Included in the WordPress Post Loop?

When you’re modifying your theme and want greater control over how the initial post appears, excluding the most recent post while displaying other articles can be useful.

Your initial post, for example, can be formatted differently or irrelevant to be included in your usual WordPress blog.

 

With that in mind, let’s look at two approaches for excluding the most recent post from the WordPress post loop.

 

Method 1: Using a New WordPress Function, remove the most recent post from the WordPress Post Loop.

Adding code to your WordPress files is the simplest way to exclude the most recent post from the post loop. Check out our instruction on how to copy and paste code in WordPress if you haven’t done it before.

The code snippet below can be placed in your functions.php file, a site-specific plugin, or a code snippets plugin.

1
2
3
4
5
6
7
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 );

 

This code prevents the most recent post from appearing on your home page loop. Only the first post will be hidden because the offset is set to one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 = 1;
    
    //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);
    }
}

The offset will be reset to 1 with this code line. However, it also has an offset and pagination. It instructs our blog archive to skip the first post in this case.

1
2
3
4
5
6
7
8
9
10
11
12
13
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
    //Define our offset again...
    $offset = 1;
    //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;
}

The final code snippet defines your offset one more time. Plus, it makes sure that pagination will work properly.

Once you’ve added the code snippets above, the latest post will now be removed from the WordPress post loop.

Method 2. Exclude Latest Post from WordPress Post Loop by Changing WordPress Theme Files

Advertisements

Another way to exclude the latest post from the WordPress post loop is by adding a single line of code to your WordPress theme files.

This achieves a similar result to the code above, but you will need to add it directly to the WordPress loop where you want it to display.

So, if you want to update the post loop for the entire site, you’d put it in the index.php file.

Note: Adding this code to your WordPress files may cause problems with your website’s pagination.

To begin, copy and paste the following code into your WordPress loop and save it.

1
query_posts('posts_per_page=6&offset=1');

 

This code makes use of the query parameter to inform the loop to only show the next five posts after the most recent one. The offset setting disables the display of the most recent post.

It will appear similar to the code snippet below because it will be placed just above your WordPress post loop.

1
2
3
4
query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) :
while ( have_posts() ) : the_post();
endwhile;

After you’ve customized and saved the file, upload it to your WordPress hosting account’s theme directory.

Use an FTP program or the file manager option in your WordPress hosting control panel to accomplish this.

 

If you’ve never used FTP before, you should read our tutorial on how to use FTP to upload files to WordPress.

 

The most recent WordPress post will be excluded from the WordPress post loop and will not appear on your blog page when the code is implemented.

 

We hope that this article has shown you how to remove the most recent post from the WordPress loop.

Advertisements

Leave a Reply