Exclude Pages from WordPress Search Results

Posted in: Newspaper
Post count: 8

Hi,

I have tried to exclude all my pages from my Search results.

I have tried the solution (http://www.wpbeginner.com/wp-tutorials/how-to-exclude-pages-from-wordpress-search-results/) you suggest (https://forum.tagdiv.com/topic/search-box-3/), but if I use it that way I’m unable to search my media in my backend.

So I have tried to exclude the admin by searching codes on the web and I found this one (source –
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts):

function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
}
}

add_action('pre_get_posts','search_filter');

But it doesn’t work. With this code, the pages aren’t excluded from the search.

Please help.

Best regards,
Alina

Post count: 20688

Hi,

You could simply use a plugin, that already has these options. Something like this for example
https://wordpress.org/plugins/search-exclude/
I believe this plugin has options to exclude particular posts and pages from the search, or in bulk even. You could give it a try and see how it goes.

Thanks

Viewing 2 posts - 1 through 2 (of 2 total)
The forum ‘Newspaper’ is closed to new topics and replies.