Skip to content

Instantly share code, notes, and snippets.

@gmmedia
Last active March 11, 2023 10:03
Show Gist options
  • Save gmmedia/89efaf56b55fe659e5bedca678f68dd6 to your computer and use it in GitHub Desktop.
Save gmmedia/89efaf56b55fe659e5bedca678f68dd6 to your computer and use it in GitHub Desktop.
<?php
/**
* Modify search query to ignore the search term in HTML comments.
*
* @param string $where The WHERE clause of the query.
* @param WP_Query $query The WP_Query instance (passed by reference).
*
* @return string The modified WHERE clause.
*/
function bp_update_search_query( $where, $query ) {
if ( ! is_search() || ! $query->is_main_query() ) {
return $where;
}
global $wpdb;
$search_query = get_search_query();
$search_query = $wpdb->esc_like( $search_query );
$where .= " AND {$wpdb->posts}.post_content NOT REGEXP '<!--.*$search_query.*-->' ";
return $where;
}
add_filter( 'posts_where', 'bp_update_search_query', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment