[wp-trac] [WordPress Trac] #56294: WordPress search finds block name in comment
WordPress Trac
noreply at wordpress.org
Mon Apr 3 14:21:26 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):
Changed the syntax from excluding strings in double quotes, now excluding
strings between < and >:
{{{#!php
<?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);
}}}
PS: The search is one of the fundamental functions in a website and should
be running without false results.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56294#comment:14>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list