By default, WordPress includes both posts and pages in its search results. However, certain pages (like checkout, thank‑you, or profile pages) should not appear in search results. On ruachost.com, you can exclude pages from WordPress search using a simple PHP function.

 

Why Exclude Pages?

  • Prevents cluttered or irrelevant search results.

  • Improves user experience by showing only blog posts.

  • Keeps private or utility pages hidden from site search.

 

Steps to Exclude Pages via PHP

  1. Log in to WordPress with an administrator account.

  2. In the dashboard, go to Appearance → Theme Editor.

  3. Select your active theme and open the functions.php file.

  4. Add the following code snippet:

    // Exclude pages from WordPress search results
    function exclude_pages_from_search($query) {
        if ($query->is_search && !is_admin()) {
            $query->set('post_type', 'post');
        }
        return $query;
    }
    add_filter('pre_get_posts', 'exclude_pages_from_search');
    
     
  5. Click Update File to save changes.

  6. Test your site’s search → Only posts will appear, pages are excluded.

 

Important Notes

  • Always back up your theme files before editing.

  • Use a child theme to preserve changes during updates.

  • This method excludes all pages from search.

  • If you want to exclude only specific pages, use plugins like Search Exclude instead.

Byla tato odpověď nápomocná? 0 Uživatelům pomohlo (0 Hlasů)

Powered by WHMCompleteSolution