Hide posts belongs to few categories in homepage

Currently I am using all the latest posts are displaying in the homepage settings. But now I need to hide few specific categories to be displayed in the homepage.

All the post belongs to those specific categories should not display in the homepage.

Anyone have an idea how should I do that ?


I am using following code to retrieve posts to display in the homepage,

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; 
$args = array(
    'paged' => $paged
query_posts( $args );
if (have_posts()) : while (have_posts()) : the_post();

Solutions Collecting From Web of "Hide posts belongs to few categories in homepage"

You can do this with pre_get_posts.
This hook is called after the query variable object is created, but before the actual query is run.

For excluding category id 32 and 39 from homepage, you can setup a function like this.

function wpse_exclude_categories( $query ) {

    if ( is_admin() )

    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-32,-39' );


add_action( 'pre_get_posts', 'wpse_exclude_categories', 1 );


Although I would strongly recommend you to use WP_Query. But you can change your code to exclude category posts with query_posts.

$args = array(
    'cat' => '-32,-33',
    'paged' => $paged