Archived topic
Excluding Specific Tag or Page from Search
9 replies · Started by yig on October 17, 2019
Hi there!
Is it possible to exclude a specific tag from wordpress search results? Also, how would it be possible to exclude specific pages from the results?
Is there a specific code for these so I can add to the functions.php file?
Thanks in advance!
Hi there,
Just to make sure, are you referring to WordPress search widget or GP's navigation search?
Let me know :)
Hi Leo,
Thanks for your response. I actually meant the GP navigation search (The one I enabled from the primary navigation menu)
I thought the one on the Off Canvas Panel was the same, but you made me realise I have it as a separate widget there. Maybe I'll just remove that one :)
Hi there,
This article might help: https://www.wpbeginner.com/plugins/how-to-exclude-specific-pages-authors-and-more-from-wordpress-search/
Specifically:
add_filter( 'pre_get_posts', function( $query ) {
if ( $query->is_search && ! is_admin() )
$query->set( 'tag', '-19' );
}
return $query;
} );
Thanks for your response Tom!
Actually I had tried that article before asking here but it gave me a functions.php error, so I thought maybe a different code is necessary for Generatepress. If that code works for you maybe it was about a plugin I use (Page Tags).
There are specific pages I wanted to exclude from the search.
As a workaround I created a new author, made it the author for these specific pages and excluded this author from the results (with the code from the same article). It worked well both for the widget and the nav search.
function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( 'author','-24' );
return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );
Nice workaround! Very similar solution :)
Hello,
How would you exclude the GP search funcionanility from searching pages?
Thanks a lot.
Any chance you can open a new topic for your question?
Thanks :)
Sure!
Thanks :)