Home / Disable / Disable Pages and Post from Appearing in WordPress Search Results
Duplicate Snippet

Embed Snippet on Your Site

Disable Pages and Post from Appearing in WordPress Search Results

Using WPCode, apply the following code snippet to effectively disable certain pages from appearing in the search results of your WordPress site. Simply insert this code into your WPCode plugin. This code empowers you to selectively exclude specific pages from showing up in search queries, refining the accuracy and value of your WordPress search results.

Code Preview
php
<?php
function exclude_pages_from_search($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post'); // Exclude pages by changing 'post' to 'page'
    }
    return $query;
}
add_filter('pre_get_posts','exclude_pages_from_search');

Comments

Add a Comment