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();

1 Answer 1


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
  • I apply this change, but the posts are still there in the homepage, I paste this page in the bottom of template function.php, is that the issue ? Jul 9, 2015 at 7:07
  • You must have a custom WP_Query for displaying post on homepage or may be cache.
    – Robert hue
    Jul 9, 2015 at 7:10
  • @JanithChinthana Did you change the values to your exact value, and are you using build in categories or a custom taxonomy. The code above does work, we all use it Jul 9, 2015 at 7:16
  • 1
    query_posts is really only for modifying the current loop and I think it overwrites the main query object. That's why you should never use query_posts. Use get_posts or WP_Query instead. You must read the differences between these three.
    – Robert hue
    Jul 9, 2015 at 7:46
  • 2
    query_posts is really only for nothing. It does overwrite the main query object, thus breaking it. Do a var_dump($wp_query) before query_posts( '&s=crap' ); and then again after it and check the results. Also do var_dump(is_search()); before and after your query_posts call. You will see how really bad query_posts really is Jul 9, 2015 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.