How to hide specific category posts on WordPress homepage?

By default, the homepage of a WordPress website will display all articles in all article categories. But sometimes, we may want to prohibit certain categories of content from being displayed on the homepage. In this article, Niuqi.com will introduce to you how to hide specific category articles on the WordPress homepage. By default, WordPress allows the creation of different content categories and tags. Sometimes we may want to create content categories that users cannot access. The WordPress backend does not provide the function of hiding certain categories of content. Below we introduce some methods that can be used. Why hide categories in WordPress?

Why Hide Categories in WordPress?

By default, The homepage of the website will display all articles in all article categories. But sometimes, we may want to prohibit the content of certain categories from being displayed on the homepage. In this article, Niuqi.com will introduce you how to .

By default, WordPress allows you to create different content categories and tags. Sometimes you may want to create content categories that are inaccessible to users. The WordPress backend does not provide the ability to hide certain categories of content. Here are some methods you can use.

How to Hide Categories in WordPress?

WordPress is a very flexible content management system with thousands of plug-ins to help us achieve a variety of functions.

To hide specific categories in WordPress, we can install Ultimate Category Excluder Plugin. To use this plugin, follow these steps:

How to hide specific category articles on WordPress homepage? -1
Install Ultimate Category Excluder Plugins

Step 1: Log in to the WordPress admin dashboard and navigate to Plugins > Add New;

Step 2: Search for "Ultimate Category Excluder”, install and activate the plugin;

Step 3: Go to Settings > Category Excluder.

Step 4: Select the article category you want to hide, and then click the "Update" button to perform the update.

How to hide specific category articles on the WordPress homepage?
Hide specific categories

Now, we have deleted the category we just selected from the homepage of the WordPress website.

How to remove a category from a widget?

If you want to hide a certain article category in the WordPress widget, you can download and install WP Categories Widget Plug-ins to achieve this.

This plugin has many features, including:

  • Display any category;
  • Hide widget title;
  • Display the number of articles in each category;
  • Show or hide article categories.

Step 1: First, download, install, and activate the plugin. After activating the plugin, click Widgets from the Appearance menu in your WordPress dashboard.

Step 2: Then, simply drag and drop the widget generated by the plugin to your sidebar or footer.

How to hide specific category articles on WordPress homepage?
Install WP Categories Widget Plugin to hide specific categories from widgets

Step 3: You can then choose to rename the widget title, hide title, hide count. Also, from the Show or All Categories drop-down menu, select the content categories you want to hide or show.

Step 4: Save your changes when you are finished.

How to hide specific category articles on WordPress homepage?
Hide specific categories of content

Hide Specific Categories in WordPress Using Code

In addition to the methods mentioned above, you can also hide specific article categories from the WordPress homepage by adding code.

Before you begin, it’s recommended to fully back up your current WordPress website. This will allow you to restore your website to its original state in case anything goes wrong.

In this method, we will make changes to the WordPress loop. All WordPress themes use a loop, and this loop can be modified using the pre_get_posts hook to achieve the purpose of hiding content from a specific category.

Using an FTP client or your server admin panel, connect to your server and go to wp-/themes directory. Open the currently activated theme folder and find .php file and download it to your local computer.

Open the functions file using an editor such as Notepad++ and add the following code to the file:

function exclude_category_home( $query ) { if ( $query->is_home ) { $query->set( 'cat', '-5' ); } return $query; } add_filter( 'pre_get_posts', 'exclude_category_home' );

In the code above, there is a $query->set( 'cat', '-5' ); where we replace "-5" with the category ID we want to hide. To find your category ID, follow these steps:

  • Log in to your WordPress dashboard;
  • Click Categories from the Article menu;
  • Hover your mouse over the category you want to hide;
  • At the bottom of the page, you can see the URL and ID.
How to hide specific category articles on WordPress homepage?
View the ID of a specific article category

For example, the ID for the Country category is 6. Therefore, replace -5 with -6 in the code, save the file, and upload it to the corresponding location.

Please follow the steps below:

  • Log in to your WordPress dashboard;
  • From Appearance click Theme Editor;
  • Select your theme on the right side above the theme files;
  • Find functions.php from your theme files and click on it;
  • Copy and paste the above code into the file and click Update.

Be sure to remember where you added the code so that you can replace the -5 value with the actual category ID.

How to hide specific category articles on the WordPress homepage?
Add code behind to functions file

Hide Categories Based on User Role in WordPress

There is also a way to hide categories for specific user roles. This is useful if your website has content that only premium users can access, for example.

In this method, we use CaPa Protect Plugin. This plugin may be old, but it still works and is compatible with the latest versions of WordPress. However, users must be edited manually one at a time.

Step 1: First, download, install and activate the plugin. Then log in to your WordPress dashboard. In the Users tab, click All Users and select the user you want to edit.

Step 2: Once in the editor, scroll down until you see the CaPa User Settings option. Here, you can hide or show categories and pages for this user.

How to hide specific category articles on the WordPress homepage?
Hide Categories Based on User Role in WordPress

Step 3: Click Update when finished to save your settings.

score

Leave a Reply

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