Exclude category posts from displaying on WordPress

This tutorial will show you how to exclude category posts from displaying on Blog page in WordPress.
Exclude category posts

  1. Log into your WordPress dashboard, and navigate to Appearance > Editor:
  2. Choose themeXYZ, where XYZ is your actual theme number in Select theme to edit drop down:
  3. Find custom-function.php file and open to edit it.
  4. You will need to get your category ID before editing this file. You should navigate to Posts > Categories:
  5. Hover over the category name (that you want to exclude from blog page) to see its ID:
  6. Add this code to the very end of the file before closing php tag ?> and replace 6 with your category ID:
    1
    2
    3
    4
    5
    6
    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'cat', '-6' );
    }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
  7. Click Update file button to save changes.
  8. If you want to exclude 2 or more categories, you should separate them with comma in the code, example:
    1
    $query->set( 'cat', '-6,-7' );
  9. Refresh your Blog page, now it doesn’t display posts from the category that was excluded in the file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s