[wp-trac] [WordPress Trac] #56294: WordPress search finds block name in comment

WordPress Trac noreply at wordpress.org
Tue Mar 21 14:16:13 UTC 2023


#56294: WordPress search finds block name in comment
--------------------------------------+------------------------------
 Reporter:  zodiac1978                |       Owner:  (none)
     Type:  enhancement               |      Status:  closed
 Priority:  normal                    |   Milestone:  Awaiting Review
Component:  Database                  |     Version:  5.0
 Severity:  normal                    |  Resolution:  maybelater
 Keywords:  needs-patch dev-feedback  |     Focuses:  performance
--------------------------------------+------------------------------

Comment (by ravishaheshan):

 @zodiac1978 thanks. With MySQL 8+ I was able to get it working with
 following

 {{{
 add_filter('posts_search', function ($search, $query) {
     if ($query->is_search()
     && !is_admin()
     && (!isset($_GET['context']) || (isset($_GET['context']) &&
 $_GET['context'] != 'edit'))
     ) {
         global $wpdb;
         $search_terms = $query->get('search_terms', []);
         foreach ($search_terms as $search_term) {
             $search_term = $wpdb->esc_like($search_term);
             $post_content_clause = $wpdb->prepare("wp_posts.post_content
 LIKE '%%%s%%'", $search_term);
             if (strpos($search, $post_content_clause) !== false) {
                 $modified_post_content_clause = $wpdb->prepare(
                     "REGEXP_REPLACE(post_content, %s, '') LIKE '%%%s%%'",
                     "\<[^<>]*{$search_term}[^<>]*\>",
                     $search_term
                 );
                 // Replace search clause for post_content with the one
 that search
                 // in tag sanitized content
                 $search = str_replace(
                     $post_content_clause,
                     $modified_post_content_clause,
                     $search
                 );
             }
         }
     }
     return $search;
 }, 10, 2);
 }}}

 Unfortunately, we can't use it since most of our projects get hosted in
 WPEngine and they don't support MySQL 8. Right now we are trying to create
 a plugin that creates a separate table with sanitized content and MySQL
 FTS and modify the search query to use that. But Relevanssi works as you
 said that's much better for us.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56294#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list