How to Create an Unlisted Tag in WordPress

Published Aug 23, 2017Last updated Aug 24, 2017
How to Create an Unlisted Tag in WordPress

Photo by Jason Blackeye via Unsplash


On your site, there may be some pages that are accessible for users that you don’t necessarily want to advertise. A good example of this would be a "Request to Cancel" survey or pages you'd like to hand-select users to Beta test on the fly. Ways to prevent that include not linking to the page from other pages and not including the page in any menus. However, another area you should consider is your search results.

The solution that I found works really well is creating an "Unlisted" tag. It's essentially a layer between "Published" and "Private." With this sweet little function, your pages, posts, and custom post types with a particular tag (change “1” to the correct ID) will be excluded from the front-end search. The second part of the conditional, !is_admin() is what stops you from going crazy when searching for pages on the admin side. 🙂

/**
 * Exclude "Unlisted" tag from search.
 */
function rose_exclude_unlisted($query) {
  if ( ($query->is_search) && (!is_admin()) ) {
    $query->set('tag__not_in', 1); // Change 1 to the ID of your "Unlisted" tag.
  }
  return $query;
}
add_filter('pre_get_posts', 'rose_exclude_unlisted');

Add that bad boy to your functions file (if it’s your own theme or child theme), or throw it in a plugin and fire it up!


If you're looking for help on implementing this or other similar functionality, please feel free to send me a message on Codementor.

Get more tips directly on my blog!

Discover and read more posts from Megan Morsie
get started