If you want to filter your wordpress homepage loop and remove some specific posts, following tip will be helpful. It shows how to  exclude specific posts from homepage loop.

From WordPress “pre_get_post” Action reference docs

This hook is called after the query variable object is created, but before the actual query is run.
The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object – no return value is necessary).

You can use “post__not_in” parameter to pass post ids as an array to exclude those from main query. And here is the code snippet you need [Ref: “Post & Page Parameters” section of “WP_Query” docs] :

<?php
add_action( 'pre_get_posts', 'wpsites_remove_posts_from_home_page' );
function wpsites_remove_posts_from_home_page( $query ) {

    if( $query->is_main_query() && $query->is_home() ) {
        $query->set( 'post__not_in', array( 37401, 37403 ) );
    }
} ?>

Post Tags


About The Author


Leave a Reply

Your email address will not be published. Required fields are marked *

home