Exclude category posts from displaying on Blog page

Exclude category posts from displaying on Blog page

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 functions.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 comment