fbpx

How to Hide Posts or Pages in WordPress

Published:
Last Updated: Oct 28, 2022

Written by

This article contains affiliate links, which means that I may receive a commission if you make a purchase using these links.

In this article, I’ll demonstrate advanced techniques to hide posts or pages in WordPress from the main blogroll, home page, archive pages, search results, and RSS feeds without a dedicated third-party plugin.

Why Hide Posts or Pages in WordPress?

There are numerous reasons why you would want to hide posts or pages from specific areas of your WordPress site. For instance, I have individual pages that should never show up in the search results of the internal search function of WordPress. Examples of such pages include the “Thank You” or “Confirmation” pages of newsletter subscription and other forms.

Additionally, I don’t want sponsored posts to clutter up my blogroll or RSS feed, and thus, I’d like to hide them in those areas.

Available Methods to Hide Posts or Pages

While searching for a solution that could help me achieve my goals, I stumbled across a few different methods to hide posts in WordPress.

Unfortunately, none of the popular solutions worked for me. For the sake of completeness, I’ll briefly mention them in this article anyway, before showing you what has worked best for me.

Post Visibility

WordPress - Post Visibility Settings
WordPress – Post Visibility Settings

When you publish a post or page in WordPress, its visibility, by default is set to Public. But you can change the visibility setting to Private, which renders the post or page inaccessible to everyone but logged in users. While this is a useful setting for specific occasions, it does not solve the problem of hiding individual blog posts or pages in only some areas of your WordPress site.

WP Hide Post WordPress Plugin

WP Hide Posts Plugin Reviews
WP Hide Posts Plugin Reviews

The third-party plugin WP Hide Post sounded very promising at first because its author claims to offer the specific features I was looking for. Upon closer inspection, I noticed a couple of issues and I decided not to use the plugin, including:

  • The plugin hasn’t been updated in over a year
  • It costs $3 per month to access all of its features
  • It got some terrible reviews lately
  • Some users have complained about performance issues

WordPress Hooks

Code Snippets Plugin
Code Snippets Plugin

As a result, I needed a better solution and came up with a couple of simple hooks that would hide individual posts and pages from the following areas in WordPress:

  • Search results
  • Blogroll / Homepage
  • Archive pages (Tag, Category, Year, Month, Day)
  • RSS feed

To accomplish that, I needed a way to add WordPress hooks without having to modify my theme or any other standard code. There are various plugins available that allow you to add code to WordPress, but I’m using one called Code Snippets. So before you do anything else, go ahead and download and activate that plugin. While you are at it, also download a plugin named Show IDs by 99 Robots. I’ll explain later what we’ll need that for.

WordPress Query Filter And Pre Get Posts

The specific hooks we need to hide posts or pages are a combination of the so-called “query” filter and “pre get posts” action. If you have never used the WordPress API and its hooks and all of this sounds scary, don’t worry. I’ll explain first the theory and then walk you through the implementation step-by-step.

What we’ll do is, create a couple of custom functions – one for each area we would like to hide posts and pages from and then feed them to the WordPress API.

The function to hide posts or pages from the WordPress internal search results looks like this:

function exclude_posts_pages_from_search( $query ) {
	if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
	/*
		61986 - Thank you Page
		69489 - Post about why the sky is blue
	*/
		$query->set( 'post__not_in', array(61986, 69489 ) );
  }

The code above takes the standard WordPress query object as an input argument and then manipulates it to exclude specific post or page IDs from the final search results. If you are not sure how you can find those IDs, don’t worry, I’ll show you later.

Note that I’ve added a comment (the text between / / to the function above to document what post or page each ID corresponds to. That makes it easier to find and remove them later.

To further customize how the function works, I’m using the following flags:

  • ! is_admin: only triggers the code that follows for regular users (i.e., Visitors) but not for logged in administrators
  • is_search: targets the search results only
  • is_main_query(): Makes sure we are dealing with the primary WordPress query

The last statement in my custom function manipulates the query by leveraging the “post__not_in” statement, followed by an array of post or page IDs that I’d like to exclude.

To inject our custom function into WordPress, we can use the add_action call that looks like this:

add_action( 'pre_get_posts', 'exclude_posts_pages_from_search' );

Hide Posts/Pages From Blog Roll

The function to hide posts or pages from the main blogroll is similar to the one we used for the search.

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

In the function above, we use is_home() instead of is_search() to check if the query is executing on the home page or main blogroll.

The matching action hook is:

add_action('pre_get_posts', 'exclude_single_posts_home');

Hide Posts/Pages From Archive Pages

Archive pages are special pages in WordPress and include the following:

  • Category pages
  • Tag pages
  • Date archives (Year, Month, Day)

To hide individual post/page IDs from such pages, you can use the following function and action hook:

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

As I’m sure you have noticed, we have replaced is_home() with is_archive(). Pretty straightforward, huh?

Hide Posts/Pages From RSS Feeds

Last but not least, the following function hides posts and pages from your RSS feed.

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

The conditional function to pay attention to is is_feed().

Combining Functions

While you certainly can use four separate functions to hide posts or pages from all the areas we discussed above, you can also combine them into a single function, which would look like this:

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

Note how I used parentheses () and the || (or) && (and) operator to hide an array of post/page IDs from the home page/blogroll, archive pages, RSS feeds and search results.

How to Hide Posts or Pages in WordPress

Now that you understand the theoretical aspect of hiding posts or pages in WordPress let get to the practical portion of this exercise!

Before we get started, double check that you have installed and activated the two plugins I mentioned at the beginning of the article.

Write down the Post/Page IDs you want to hide

WordPress Post IDs
WordPress Post IDs

Then go to your Posts inside of WordPress Admin and take note of the post IDs you’d like to hide. If you don’t see the ID column, click on the Screen Options drop-down at the top of your screen and make sure “ID” is selected. If you don’t see an option for “ID,” chances are, you haven’t installed or activated the Show IDs plugin.

Create a Code Snippet

Code Snippets to hide Posts
Code Snippets to Hide Posts

Next, navigate to the Snippets dashboard and click on “Add New.” Then paste one of the snippets from above, for instance, the combined snippet and replace the IDs with your own.

Once done, click on the “Save Changes and Activate” button and confirm that you don’t see any syntax errors. If you would like to deactivate or disable a snippet, simply toggle the switch in front of the snippet’s name.

Last but not least, open a new browser window and test the results. If you have used the is_admin() function, you may have to open the browser window in incognito mode for your code snippet to kick in.

Frequently Asked Questions

How Do I Know If My Code Snippets Work?

To verify that your code snippets work, check your blog roll, and the relevant archive pages (tags, categories) associated with the posts or pages you hid. That’s relatively straightforward.

To find out if you have successfully excluded individual pages from search is also easy, even if you don’t have a search box on your webpage. You can easily simulate a search by using the ?s=search+term suffix. For example, if I want to search this blog for “How to hide posts,” I can hit the following URL in my browser:

https://michaelkummer.com/?s=how+to+hide+posts

If I don’t see any relevant results, I know it worked. Depending on if you intended to hide certain posts or pages only from users who are not logged in to the WP backend, you have to test using a separate browser window running in incognito (or private browsing) mode.

In other words, make sure that you’re not accidentally logged into WordPress. Otherwise, you might still see the hidden posts.

Does Hiding Posts or Pages Impact SEO?

It depends! If you have submitted a sitemap to your target search engines (i.e., Google), and if the hidden posts or pages are still in your sitemap, indexable and not set to NoIndex, hiding them from search or other areas of your page will likely not impact SEO.

However, if SEO is your goal, then why hide them on your page? That doesn’t make sense unless you have a particular use case that I don’t know about.

I Want to Create a Private Page For Clients, How Can I Accomplish That?

If you want to create a private page for individual clients that should not be visible to anyone else, including search engines, then the above methods are probably not the best method of accomplishing that.

Of course, you could use the above snippets to hide that page and then remove the page from your sitemap, and set it to NoIndex, using a plugin, such as Yoast.

Alternatively, you could set the page Visibility to Private or Password Protected and either create user accounts for your clients or give them the password.

For more advanced scenarios, you could try a membership plugin.

Conclusion

The WordPress API and its hooks are incredibly powerful tools in your tool belt. And now that you know how to Hide Posts and Pages in WordPress, I’m sure you’ll find other use cases for them to customize your site. If you have any questions or concerns, leave a comment below, and I’ll do my best to answer them.

22 thoughts on “How to Hide Posts or Pages in WordPress”

  1. Hi Michael

    Thanks for the post.
    How will this code change if I want:
    -> Users to register
    -> Select categories they prefer / or exclude those they don’t (Similar to Spotify sign up process)
    -> Save to user profile
    -> Home page for them is now personalised to their categories they have chosen
    -> and every time they log in it remembers their custom home page

    Much like the plugin, but personalised for each user (when logged in), so different home page for every user based on their selection.

    Reply
    • Hi Max,

      What you’re trying to accomplish goes way beyond a simple snippet. You’d have to create a plugin for that or find an existing membership plugin that can do that.

      Cheers,
      Michael

      Reply
  2. Hey

    Not sure if you can help me. I’ve been looking everywhere for days for information about having more control over my posts/categories. I want to hide certain posts from certain posts, even if they share categories (not sure if that makes sense).

    All the information I can find online is to hide a post from search, RSS or the blog page. And all the code seems to be site/theme-wide and not for individual posts. I’m sure there’s an easy way to do this, but I just can’t find anyone writing about it anywhere.

    Any clues? Do I even make sense?

    Reply
  3. Hello thank you for the great tips.

    I am interested in hiding a page that is not to be available when you land on the page. Yet, would like tbe page to be indexed and crawled and being searchable by the search engines and SEO.

    How do I go about that? Make the page private, yet somehow add the page in the sitemap.xml file or add the address as an independent sitemap.xml?

    Thanks

    Reply
    • What you could do is check for the user agent to determine if a search engine bot or a regular browser is viewing the page. Based on that, you can show the regular content, or something else.

      Show Only this codes
      }
      else
      {
      // Show normal website
      }
      ?>

      Reply
    • Hi Nick!

      I’m pretty sure it is. To be clear, do you want do hide all posts of a certain category? So if a visitor clicks on /categoryA/ then he/she would see an “empty” category roll?

      Cheers,
      Michael

      Reply
  4. Hi thanks,
    Perfect for my need – to hide an alternate copy of a page for a dev testing purposes. Set to no index and I trust no one will get there unless they have the address, but still allows for live metrics testing.
    Awesome!

    Reply
  5. how can i use this plugin for custom feed. i need to use hide feed post plugin on my custom feed. I want to use hide post checkbox for my custom feed so please tell me how is it possible?

    Reply
  6. Hi Michael,
    Thanks heaps for your article. I suppose it will help many people, including myself. I have had only one issue which I am unsure whether it is due to my lack of programming knowledge/skills or whether these functions do not provide what I want from them.
    I am launching a blog which all the content is written in two languages. I add a button under the title of each post which switches from one language to the other. Thus, I wanted to hide posts in one language from the main page but leave it active so as to allow access to content in both language. When I use these codes, it excludes the hidden content altogether.
    Would you be able to point me to the right direction as to achieve my goal?
    Thanks in advance mate.

    Reply
    • Hi Thali,

      That depends on how your language integration works. Are you using a plugin or do you have any other means to identify the language of a particular post?

      Depending on that, you can probably tweak the query filters to hide posts that match a particular language tag.

      Cheers,
      Michael

      Reply
  7. Thanks for the post! I believe it is what I’m looking for but it hasn’t worked out exactly right so far. I have a list of posts that I want hidden on my posts page which is separate from my home page. I don’t want these articles listed there because they are linked to a drop down menu that is on the original post. These posts are all hidden but when I click on them from the menu they are linked to they don’t show up at all. Is there any way you could help me figure out how to do what I’m trying.

    Reply
  8. Thanks a lot for this awesome post! I have been looking on Google for a guide on how to hide posts but only found old articles which solely suggested the WP Hide Post Plugin which is outdated (no update since 2 years) and not doing the job as you also pointed out. I’m very happy that I found your article when I was just about to give up. It should be #1 in the Google search rankings!

    Reply
    • Hi Akwasi,

      Thanks for your comment, I appreciate it! May I know what search term you used to find my blog. If I knew the term, I can further optimize my article to improve its chance of ranking higher up.

      Cheers
      Michael

      Reply
  9. I searched high and low for a post like this. I hope it works.
    I am a wedding photographer and want a private page for clients but need it hidden from Search engines etc.
    1. Stupid question but how can i check if this works?
    2. The main reason i send clients to this private page is for seo. Will this method hamper my cause?

    Great work and thank you. Will implement later.

    Reply

Leave a Comment