Excluding Specific Pages from Search Results

WordPress search function includes all of its pages and posts by default. However, if you think you need to exclude specific pages from your search result, you can do that very easily. Simply copy and paste the following snippet on your theme’s functions.php file and update the page.

function exclude_specific_pages($where = '') {
   if (is_search()) {
     $exclude = array(xx,xx,xx,xx);	
     for($x=0;$x<count($exclude);$x  ){
       $where .= " AND ID != ".$exclude[$x]; } }
      return $where; }

However, don’t forget to change the “xx” with your page ID. You can add as many pages as you want within the array.

Today In History



Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.