Exclude Pages from Search

User
Is there a way to stop certain pages/posts from showing up in the results?
Wordpress search
Assistant
Yes, you can exclude specific pages or posts from the WordPress search results using several methods. Here are some common approaches:

### Method 1: Using the `pre_get_posts` Hook

You can add custom code to your theme's `functions.php` file (or a custom plugin) to exclude specific pages or posts from search results using their IDs. Here's how:

function exclude_pages_from_search($query) {
    if ($query->is_search && !is_admin()) {
        // List of post/page IDs to exclude
        $exclude_ids = [123, 456, 789]; // Replace with your post/page IDs
        $query->set('post__not_in', $exclude_ids);
    }
    return $query;
}
add_action('pre_get_posts', 'exclude_pages_from_search');

### Explanation
– **`$query->is_search`** checks if the current query is a search query.
– **`!is_admin()`** ensures the exclusion only happens on the front end, not in the admin area.
– **`post__not_in`** parameter is used to exclude specific post/page IDs.

### Method 2: Exclude Posts by Category or Post Type
If you want to exclude entire categories or post types, you can modify the `pre_get_posts` function like this:

**Exclude Posts from a Specific Category:**

function exclude_category_from_search($query) {
    if ($query->is_search && !is_admin()) {
        $query->set('cat', '-5'); // Replace '5' with your category ID to exclude
    }
    return $query;
}
add_action('pre_get_posts', 'exclude_category_from_search');

**Exclude Posts of a Certain Post Type:**

function exclude_custom_post_type_from_search($query) {
    if ($query->is_search && !is_admin()) {
        $query->set('post_type', ['post', 'page']); // Only include certain post types
    }
    return $query;
}
add_action('pre_get_posts', 'exclude_custom_post_type_from_search');

### Method 3: Using a Plugin
If you're not comfortable editing code, you can use a plugin to exclude posts or pages from search results. Some popular options include:

– **Search Exclude**: This plugin adds a checkbox on the post edit screen to exclude posts from search results.
– **Relevanssi**: A powerful search plugin with options to include/exclude posts, pages, and custom post types.

### Method 4: Using `noindex` Meta Tag
If you only want to prevent certain pages or posts from appearing in search engines (but still allow them in WordPress search), you can use the `noindex` meta tag:

function add_noindex_meta() {
    if (is_page([123, 456]) || is_single([789])) { // Replace with your IDs
        echo '<meta name="robots" content="noindex, follow">';
    }
}
add_action('wp_head', 'add_noindex_meta');

### Summary
– Use the **`pre_get_posts`** method for flexible control over what appears in WordPress search results.
– Use plugins if you prefer a user-friendly interface.
– Use the **`noindex`** meta tag to control how pages are indexed by search engines.

Choose the method that best fits your needs!

User
We will check this issue But I’m sure that its because of low resources like memory limit / cpu usage.
Correct above sentence
Assistant
We will look into this issue, but I believe it is likely due to low resources, such as insufficient memory or high CPU usage.

Leave a Comment

Your email address will not be published. Required fields are marked *