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

WordPress Trac noreply at wordpress.org
Sun Apr 2 16:41:15 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 espiat):

 Replying to [comment:8 zodiac1978]:
 > > But by default, it doesn't remove Gutenberg annotations from the
 content
 >
 > IIRC Relevanssi is building an index and this does not contain those
 comments. The content itself is untouched, and the search is not using the
 content anymore, but the index.
 >
 > In my tests I couldn't find block names with Relevanssi used.
 >
 > At the moment I try to get this at least working with MySQL 8.0.4 or
 above (where REGEXP_REPLACE was introduced), but couldn't get this to
 work. My latest try:
 >
 > {{{
 > $where .= " AND REGEXP_REPLACE({$wpdb->posts}.post_content,
 '\<\!\-\-.*?\-\-\>', '') LIKE '%{$search_query}%'";
 > }}}
 >
 > This does not throw any error but is still not working ... :(


 Hi.

 With this

 {{{
 > $where .= " AND REGEXP_REPLACE({$wpdb->posts}.post_content,
 '\<\!\-\-.*?\-\-\>', '') LIKE '%{$search_query}%'";
 }}}

 i find class names, too.

 So i changed it. Now class names arent in the search results.


 {{{#!php
 function tl_custom_search_query($search, $query)
 {
     if (!is_search() || !$query->is_main_query()) {
         return $search;
     }
     global $wpdb;
     $search_query = get_search_query();
     $search_query = $wpdb->esc_like($search_query);
     // Remove the original search condition
     $search = preg_replace("/\({$wpdb->posts}.post_content LIKE
 '%[^%]+%'\)/", "", $search);
     // Add a custom search condition
     $search .=  " AND
 REGEXP_REPLACE(REGEXP_REPLACE({$wpdb->posts}.post_content, '\"[^\"]*\"',
 ''), '<!-.*?->', '') LIKE '%{$search_query}%'";
     return $search;
 }
 add_filter('posts_search', 'tl_custom_search_query', 10, 2);
 }}}


 local test system:
 mysql 8.0.16
 nginx
 php 8.0.0

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


More information about the wp-trac mailing list