- This topic has 9 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
January 20, 2023 at 4:31 am #2502696
Nicole
Is there a way to exclude specific pages from the internal search?
Ideally, I would like to exclude the pages that are set to no index if possible.
If not, then I would like to exclude specific pages by URL.Thanks in advance!
January 20, 2023 at 5:19 am #2502753David
StaffCustomer SupportHi there,
you can exclude pages with this snippet and adding their IDs in the
array`add_filter( ‘pre_get_posts’, ‘db_exclude_pages_in_search’ );
function db_exclude_pages_in_search($query) {
if ( $query->is_search )
$query->set( ‘post__not_in’, array( 1, 2, 3, 4, 5 ) );return $query;
}How are the pages being set to
noindex? it might be possible to get their ids so you don’t have to.January 20, 2023 at 5:32 am #2502768Nicole
Thanks so much for the quick reply. They are being no indexed via Yoasts SEO plugin.
January 20, 2023 at 5:57 am #2502797David
StaffCustomer SupportTry this function:
function be_exclude_noindex_from_search( $query ) { if( $query->is_main_query() && $query->is_search() && ! is_admin() ) { $meta_query = isset( $query->meta_query ) ? $query->meta_query : array(); $meta_query['noindex'] = array( 'key' => '_yoast_wpseo_meta-robots-noindex', 'value' => 1, 'compare' => 'NOT EXISTS', ); $query->set( 'meta_query', $meta_query ); } } add_action( 'pre_get_posts', 'be_exclude_noindex_from_search' );For reference and kudos, it came from this site:
https://www.billerickson.net/code/exclude-no-index-content-from-wordpress-search/
January 20, 2023 at 6:47 am #2502856Nicole
Thank you so much!
January 20, 2023 at 6:48 am #2502859David
StaffCustomer SupportGlad to be of help!
January 20, 2023 at 6:49 am #2502864Nicole
Sorry, one last question. Is this a snippet or a hook? I’m not sure where to put the code…
January 20, 2023 at 7:11 am #2502896David
StaffCustomer SupportIts a PHP Snippet.
This doc explains how to add the code:
https://docs.generatepress.com/article/adding-php/TLDR: Are you using a Child Theme?
If Yes, then you can add the code to your Child Themes functions.php
If No, then use the Code Snippets plugin ( link in the above doc ) and Add New Snippet for the code.January 20, 2023 at 8:33 am #2503110Nicole
Thanks very much!
January 20, 2023 at 11:04 am #2503313David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.