{"id":15636,"date":"2020-11-09T15:00:55","date_gmt":"2020-11-09T14:00:55","guid":{"rendered":"https:\/\/qodeinteractive.com\/magazine\/?p=15636"},"modified":"2020-11-06T13:53:18","modified_gmt":"2020-11-06T12:53:18","slug":"exclude-specific-results-from-wordpress-search","status":"publish","type":"post","link":"https:\/\/qodeinteractive.com\/magazine\/exclude-specific-results-from-wordpress-search\/","title":{"rendered":"How to Exclude Specific Results from WordPress Search"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><p>[vc_row][vc_column][vc_column_text]When trying to give your website\u2019s visitors a way to navigate your website, the two elements most worthy of your attention are the navigation menu and the search functionality. <a href=\"https:\/\/qodeinteractive.com\/magazine\/how-to-add-navigation-menu-in-wordpress\/\">Adding the navigation menu<\/a> can be a breeze. The search bar is just <a href=\"https:\/\/qodeinteractive.com\/magazine\/add-widget-to-wordpress\/\">another widget you can easily add<\/a>. Keep in mind, however, that both require fine-tuning to shine fully.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]When it comes to the search bar, your first instinct might be to reach for a <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpress-search-plugins\/\">WordPress search plugin<\/a> and use it to improve on the native search functionality. Before you do that, however, you could ensure you\u2019ve exhausted other options you have in WordPress. Excluding search results is one of them.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]In this article, we\u2019ll show you:[\/vc_column_text][vc_empty_space height=&#8221;22px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><a href=\"#reasons-to-exclude-specific-results\">The Reasons to Exclude Specific Results from Search<\/a><\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;5px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><a href=\"#how-to-exclude-posts-and-pages\">How to Exclude Posts and Pages from Search Results<\/a><\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;5px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><a href=\"#how-to-exclude-authors\">How to Exclude Authors<\/a><\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;5px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><a href=\"#exclude-tags-categories-and-custom-taxonomy-terms\">How to Exclude Tags, Categories, and Custom Taxonomy Terms<\/a><\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\"><a id=\"reasons-to-exclude-specific-results\"><\/a>Why Would You Want to Exclude Specific Results from Search?<\/h2>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;24px&#8221;]<div class=\"qodef-single-image-holder    \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"400\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Why-Would-You-Want-to-Exclude-Specific-Results-from-Search.jpg\" class=\"attachment-full size-full\" alt=\"Why Would You Want to Exclude Specific Results from Search\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Why-Would-You-Want-to-Exclude-Specific-Results-from-Search.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Why-Would-You-Want-to-Exclude-Specific-Results-from-Search-300x171.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Why-Would-You-Want-to-Exclude-Specific-Results-from-Search-620x354.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]<strong>The primary role of the search function is to produce results based on a user\u2019s query.<\/strong> If you want the feature to be useful and add to the overall user experience, you\u2019ll want the results to be as relevant and zeroed-in as possible.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]One of the ways you can improve relevance is by removing any results that are usually irrelevant, such as the home page or the about us page. <strong>You can also use the search results exclusion to reduce the clutter<\/strong>. You can allow your visitors to search only several posts and pages, instead of the whole website.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Finally, excluding specific results can help you manage access to the content on your website. If there are posts or pages, you don\u2019t want to be readily found, <strong>excluding them from the navigation and the search results would be an excellent way to hide them without actually hiding them. <\/strong>[\/vc_column_text][vc_empty_space height=&#8221;80px&#8221;][vc_widget_sidebar sidebar_id=&#8221;new-top-picks-banner&#8221;][vc_empty_space height=&#8221;81px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\"><a id=\"how-to-exclude-posts-and-pages\"><\/a>How to Exclude Posts and Pages from Search Results<\/h2>\n<p>[\/vc_column_text][vc_column_text]You can use a plugin to exclude specific posts and pages from your website\u2019s search results. It\u2019s called <a href=\"https:\/\/wordpress.org\/plugins\/search-exclude\/\" target=\"_blank\" rel=\"noopener noreferrer\">Search Exclude<\/a>, and you can find it easily by navigating to <em><strong>Plugins &gt; Add New<\/strong><\/em> in your website\u2019s backend and typing its name in the search box. <strong>Once you find it, install and activate it.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude.jpg\" class=\"attachment-full size-full\" alt=\"Search Exclude\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]Once activated, you\u2019ll notice that your existing posts and pages in your backend have a new Search Exclude option at the bottom of the right-hand side menu.<strong> By checking the Exclude from Search Results box, you can exclude specific posts and pages from search results.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Post.jpg\" class=\"attachment-full size-full\" alt=\"Search Exclude Post\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Post.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Post-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Post-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]If you want to access the full list of pages and posts you\u2019ve excluded from search results using the plugin, you can find your way to <em><strong>Settings &gt; Search Exclude<\/strong><\/em>. You\u2019ll find the list there, and you\u2019ll also be able to<strong> remove posts and pages from the list by unchecking the appropriate box and clicking on the Save Changes button. <\/strong>[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Items.jpg\" class=\"attachment-full size-full\" alt=\"Search Exclude Items\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Items.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Items-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Search-Exclude-Items-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]As plugins go, this one is simple and lightweight, but also pretty short on features. There are a couple more ways you can exclude search results that the plugin doesn\u2019t cover. <strong>And you know what that means \u2014 it\u2019s custom code snippets time!<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\"><a id=\"how-to-exclude-authors\"><\/a>How to Exclude Authors<\/h2>\n<p>[\/vc_column_text][vc_column_text]To exclude from search results posts and pages made by specific authors, <strong>you\u2019ll have to add some custom code to your website<\/strong>. The two methods we\u2019d recommend for this are <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpress-create-child-theme\/\">creating a child theme<\/a> and adding the code to its <em><strong>functions.php<\/strong><\/em> file or using the <a href=\"https:\/\/wordpress.org\/plugins\/code-snippets\/\" target=\"_blank\" rel=\"noopener noreferrer\">Code Snippets<\/a> plugin without the child theme.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<strong>Either way, you\u2019ll need to find the author\u2019s ID first.<\/strong> To do that, you can navigate to <em><strong>Users &gt; All Users<\/strong><\/em>, and hover over the author you want to exclude with your mouse. In the bottom right-hand corner, you\u2019ll notice a link appear \u2014 <strong>that link holds the author ID number<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/User-ID.jpg\" class=\"attachment-full size-full\" alt=\"User ID\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/User-ID.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/User-ID-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/User-ID-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]With that number in mind, you can go ahead and add the following code using your preferred method:<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function my_search_filter( $query ) {\r\nif ( $query-&gt;is_search &amp;&amp; !is_admin() )\r\n$query-&gt;set( 'author','-5' );\r\nreturn $query;\r\n}\r\nadd_filter( 'pre_get_posts', 'my_search_filter' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Instead of the number 5, you should insert the ID number of the author you want to be excluded from the results. <strong>If you want to exclude multiple authors, list all their ID numbers separated by commas<\/strong>. It\u2019s as simple as that.[\/vc_column_text][vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\"><a id=\"exclude-tags-categories-and-custom-taxonomy-terms\"><\/a>How to Exclude Tags, Categories, and Custom Taxonomy Terms<\/h2>\n<p>[\/vc_column_text][vc_column_text]When excluding tags and categories, your first step should be the same \u2014 finding out the ID number of the category or tag that you want to exclude. Head over to <em><strong>Posts &gt; Categories<\/strong><\/em> for categories, or <em><strong>Posts &gt; Tags<\/strong><\/em> for tags, and do the same thing you did to find out the author\u2019s ID \u2014 <strong>hover over the category or tag you want to exclude. You\u2019ll find the ID number in the link that appears in the bottom right-hand corner.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Tag-ID.jpg\" class=\"attachment-full size-full\" alt=\"Tag ID\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Tag-ID.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Tag-ID-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/11\/Tag-ID-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]If it\u2019s a category you want to exclude, you can use the following code:<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function my_search_filter( $query ) {\r\nif ( $query-&gt;is_search &amp;&amp; !is_admin() )\r\n$query-&gt;set( 'cat','-21' );\r\nreturn $query;\r\n}\r\nadd_filter( 'pre_get_posts', 'my_search_filter' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Of course, you should replace the number 21 with the correct ID for your category.<strong> Just like with the authors, you can exclude multiple categories by listing their ID numbers separated by commas.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]For tags, the code looks almost exactly the same:[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function my_search_filter( $query ) {\r\nif ( $query-&gt;is_search &amp;&amp; !is_admin() )\r\n$query-&gt;set( 'tag','-24' );\r\nreturn $query;\r\n}\r\nadd_filter( 'pre_get_posts', 'my_search_filter' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<strong>Again, you should ensure to include the proper ID number for your tag and use the correct method for excluding more tags.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]You\u2019ll have to use slightly different code if you want to <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpress-taxonomy\/\">add custom taxonomies<\/a> to the excluded bunch:[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function my_search_filter( $query ) {\r\nglobal $wp_the_query;\r\nif( $query === $wp_the_query &amp;&amp; $query-&gt;is_search() ) {\r\n$tax_query = array(\r\narray(\r\n'taxonomy' =&gt; 'mammals',\r\n'field' =&gt; 'slug',\r\n'terms' =&gt; 'sloths',\r\n'operator' =&gt; 'NOT IN',\r\n)\r\n);\r\n$query-&gt;set( 'tax_query', $tax_query );\r\n}\r\n}\r\nadd_action( 'pre_get_posts', 'my_search_filter' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Instead of \u201cmammals,\u201d you\u2019ll have to provide the custom taxonomy you\u2019ve set. The \u201csloths\u201d part stands in for the specific terms you want to exclude from the search results.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">Let\u2019s Wrap It Up<\/h2>\n<p>[\/vc_column_text][vc_column_text]Being able to exclude specific results from a WordPress search can come in handy. The best-case scenario is that it saves you from having to install a search plugin. Usually, however, it will give you some control over who can find what on your website, and it\u2019s up to you to make the most of it. Just remember that, in this case, third-party plugins will only get you halfway to your goal. You\u2019ll have to traverse the rest on your own, one line of code at a time.<br \/>\n[\/vc_column_text][\/vc_column][\/vc_row]<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Need a way to exclude search results from your WordPress website? It&#8217;s as easy as pie &#8211; and we&#8217;ll show you exactly how to do it!<\/p>\n","protected":false},"author":9295,"featured_media":15656,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[21,15,4,13],"class_list":["post-15636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-php","tag-plugins","tag-tips","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/15636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/users\/9295"}],"replies":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/comments?post=15636"}],"version-history":[{"count":0,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/15636\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media\/15656"}],"wp:attachment":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media?parent=15636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/categories?post=15636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/tags?post=15636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}