Hiding WordPress Pages and Posts: What to Know

Hide our WordPress pages or posts? Why would our organization want to do that?
I know. At first glance, it seems a little odd.
Isn’t the whole purpose of your website to publish information and make it accessible to your community? What good does hiding it do?
There’s a number of instances where you may want to publish information on your website, but not make it accessible everywhere – or to everyone.
In our experience, most organizations who want to hide content really just want to limit access to content to the right people or via the right distribution methods. For example:
- You may want to publish a recording of a webinar you hosted, but you only want it accessible to people who registered for it
- If you’ve created a piece of lead generation content, you usually don’t want it freely available – someone needs to fill out a form to download it
Here’s how you can hide your WordPress pages and posts to ensure access only to the right people.
What Does Hiding Content Actually Mean?
Hiding content generally refers to ensuring that your specified pages or posts don’t appear in your site search results, in your list of latest posts/blog, on your front page, or in any of your site’s RSS feeds.
Typically, when your content is hidden, then it can only be accessed by a direct link – either by typing the URL into your browser or clicking a link directly to the hidden page or post.
While there are certainly ways to “hide” content until a user has taken a specific action – like entering a password or filling out a form – we’re going to focus on ways to prevent your WordPress page or post from showing up in the typical places it would be accessible.
Use a Plugin: WordPress Hide Posts
The WordPress Hide Posts plugin does exactly what you’d think it should: it gives you the option to select where a specific piece of content should (and shouldn’t) be available.
When the plugin is activated, you’ll see this box appear on individual posts (and, if selected, Pages):
Check the boxes where you want to ensure the post doesn’t appear, and save.
It’s as simple as that.
The plugin makes the most sense to use when you have specific pages or posts that you need hidden in specific (and sometimes differing) ways. Meaning, if you have a post that should be hidden everywhere – and another post you simply just don’t want to show up on your homepage – this is a good way to differentiate and achieve both goals.
Use Code: Create a Tag and Add a Function
The alternative to using a plugin is to code the functionality yourself. It’s not as hard as you think, and can be easier than using the plugin – once you have everything set up.
This method requires creating a Tag that you can added to any piece of content; when WordPress does its thing, if it sees that tag, it’ll ensure your content isn’t showing.
In the below code, we’ve create a tag called “Hidden”. It has ID 704, which we’ll use to hide items with that tag from search results and any WordPress queries.
/* Hide from Search Results */
function dgtlnk_hide_from_search($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('tag__not_in', '704'); // "Hidden" Tag
}
}
}
add_action('pre_get_posts','dgtlnk_hide_from_search');
function dgtlnk_hide_from_loop($query) {
if ( !is_admin()) {
$query->set('tag__not_in', 704);
}
}
add_action('pre_get_posts','dgtlnk_hide_from_loop');
// add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
// ensure all tags are included in queries
function tags_support_query($wp_query) {
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}
// tag hooks
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');
You’ll notice a couple of other functions included here: tags_support_all
adds support for tags to Pages, and tags_support_query
ensures all of a piece of content’s tags are added to the query.
With these few lines of code, you can hide any piece of content on your site simply by adding the tag “hidden” to it.
Have questions about the code to hide your WordPress pages and posts or the plugin that does the same? Reach out and let us know.