Placeholder canvas

The Easy Guide To Hide Posts or Pages in WordPress

In this WordPress tutorial, you will learn how to hide posts or pages in WordPress. It is an easy process and you don’t need a plugin to do that. In fact, most of the articles on the internet recommend the use of the plugin WP Hide Post. We do not recommend the use of this plugin. The plugin has not been updated since 2017. Also, it is not tested with the last 3 major WordPress releases. We will show you how you can hide posts or pages without using any WordPress plugin.

With this easy WordPress guide, you will be able to hide WordPress posts and pages from the home page, blogroll, archive, search, and RSS feeds. You can also hide pages or posts for those users who are not logged in. You may also set a password for your posts and pages so that they will be hidden from users who do not have those credentials. Let’s see how we can accomplish these things.

Hide Posts Using WordPress Visibility Settings

By default, WordPress gives you the option to set visibility for your Page or Post. The visibility setting is available just below the Publish button.

WordPress Post Visibility Hide Posts or Pages in WordPress

 

It has the following options:

  • Public: This setting lets everyone see your post or page. Your content is visible to everyone.
  • Private: With this setting, you can limit the visibility of your post or page to site admins and editors. This means your post will be hidden from users who do not have admin or editor access. Since you can assign these roles, you can control who can see your post or page.
  • Password Protected: You can set a password for your post or page. The post will be hidden from all users who do not have the password. You can share your password when you want others to see your content.

These three are very powerful and easily accessible methods that you can use to hide your WordPress posts and pages. You don’t need to be stuck with these settings as you can choose these options every time you publish. Thus, you can have different visibility settings for different posts or pages.

Read on if you need advanced control than what is available in the visibility settings. We will explore how specific we can get about hiding WordPress posts and pages. If you are trying to keep your staging site private and away from search bots, there is a better option. All you need to do is to login into your managed WordPress hosting control panel and password protect the entire site. This way, you don’t need to repeat the following steps for each post or page.

Use Category to Hide Posts or Pages in Bulk

You can create a category for pages that you want to hide. For example, You can create a category called “Hidden” for the posts that you want to hide from the public.

Just assign this category to the posts you want to hide.

We will now see how we can hide all the post from a specific category.

Before we get to that, you will need to have Category ID and Post ID. How do you find that? Use the plugin Show IDs by Echo Plugins. It is a simple plugin that displays IDs of posts, pages, and categories. As we are going to use these IDs extensively in this WordPress tutorial, make sure you have installed this plugin on your WordPress site.

Once activated, the plugin will show IDs like this:

Show ID Hide Posts or Pages in WordPress

Once you have the category ID, insert the following code snippet in your theme’s functions.php file.

function exclude_category($query) {
 if ( $query->is_home() ) {
 $query->set( 'cat', '- Category ID' );
 }
 return $query;
 }
 add_filter( 'pre_get_posts', 'exclude_category' );

Make sure you put Category ID that you found using the plugin in the field “Category ID”. For example, if your category ID is 5, the code snippet will look like this:

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

This code will prevent all the pages or posts in this category from appearing on the home page and blog page.

 

 

Hide Posts or Pages from Specific Areas of your WordPress site

You can decide to hide your posts or pages from the blogroll only. It may be desirable not to show them in the search results. You may want them to disappear from archive pages. Can you do this? Yes, you can. In this section, we will show you how you can hide posts or pages from these specific areas of your WordPress website.


1. Archive Pages

Archive pages include category pages, tag pages, date archives, etc. You can use the following code to hide individual post or page from archive pages:

function exclude_single_posts_archive($query) {
	 if ($query->is_archive() && $query->is_main_query()) {
		  $query->set('post__not_in', array(1001, 1002,1003));
	  }
}
add_action('pre_get_posts', 'exclude_single_posts_archive');

The numbers 1001, 1002, 1003 are Page or Post IDs. You should replace them with those you want to hide.

2. Blogroll

Use the following code to hide posts and pages from the blogroll:

function exclude_single_posts_home($query) {
	 if ($query->is_home() && $query->is_main_query()) {
		  $query->set('post__not_in', array(69489, 69177,68878,68736));
	  }
}
add_action('pre_get_posts', 'exclude_single_posts_home');

3. Search Pages:

Use the following code to hide posts from the Search pages:

function exclude_posts_pages_from_search( $query ) {
	if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
	
		$query->set( 'post__not_in', array(1001, 1002 ) );
  }
add_action( 'pre_get_posts', 'exclude_posts_pages_from_search' );

The numbers 1001, 1002 stand for Page or Post IDs. Replace them with the ones you want to hide.

4. RSS Feeds

The following code will hide your posts or pages from RSS feeds:

function exclude_single_posts_feed($query) {
	 if ($query->is_feed() && $query->is_main_query()) {
		  $query->set('post__not_in', array(69489, 69177,68878,68736));
	  }
}
add_action('pre_get_posts', 'exclude_single_posts_feed');

Concluding Thoughts

While WordPress provides Visibility options by default, you can extend these with the help of a few snippets of code. We have seen how we can hide posts and pages on your WordPress website in this easy guide. You do not really need a third party plugin to accomplish this task. A plugin like WP Hide Post was popular in the past but it is no longer updated. We recommend using these custom codes instead.

We hope this article helped you. If you have any queries, please leave your comments.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.