Remove Homepage from Site Search

How can I remove the homepage from appearing in site search?
It tends to appear as the last search result and whats below is all code. How can I remove it from the internal search but of course keep the page from appearing on external search engines?

Hey Ashley,

Please install and activate the child theme and login through FTP then edit the functions.php then add this code:

function exclude_pages_from_search($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post__not_in', array(XXXX));
    }
  }
}

add_action('pre_get_posts','exclude_pages_from_search');

Please change XXXX in array(XXXX) in the code above to the ID of the homepage.

Here’s how you can locate the ID of a page:

Hope this helps.

Perfect! Thank you so much!

You’re welcome. Glad we’re able to help.

@christian_y - actually have a bigger problem now. After I just updated the site files (to hopefully fix a portfolio issue) - the site search is now malfunctioning completely.
If you search anything, it just repeats the results forever. I removed the code you gave me thinking that could have been it.
The results are still the same. Can you please help?

Hi again, I did a search and looks like I solved the problem through this solution.
As I understand it this is a temporary fix. Can you please tell me when this will be totally fixed?

Hey Ashley,

Once the fix is up, it will be listed in our Changelog at http://theme.co/changelog

Please stay tuned for updates.

We’re sorry for the inconvenience.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.