How to exclude pages from WP search based on page title

I need to disable our site’s gated content from showing up in search results. Currently, I’m using the following code to exclude pages by post ID:

// Exclude gated content (download pages) from site search

function mm_search_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( ‘post__not_in’, array( 18321, 17218, 17212, 17217, 17136, 16269, 17484, 7349, 9264, 15919, 15920, 15921, 15922, 7939, 15930, 16700, 8923, 15820, 18489, 18234, 15970, 18235, 18236, 16065 ) );
}
}
add_action( ‘pre_get_posts’, ‘mm_search_filter’ );

This seems a little clunky though and as our gated content pages grow, so will this list. So my question is, is there a way to do the same thing by excluding page titles that start with a certain string value? To further clarify my situation, all of the pages i’m currently excluding have page titles that start with ‘download-‘ :

download-marketing-roadmap
download-social-media-field-guide
download-big-book-of-working-smarter
etc, etc, etc….

Would greatly appreciate a solution to this!

Read more here:: How to exclude pages from WP search based on page title

Leave a Reply

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