Skip to content

Commit b247fdc

Browse files
committed
Allow "quoted strings" to be used in searches. mega free-beer-when-I-finally-meet-you-in-person props to ringmaster. fixes WordPress#3177
git-svn-id: https://develop.svn.wordpress.org/trunk@4426 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1d570ec commit b247fdc

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

wp-includes/query.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -743,29 +743,26 @@ function &get_posts() {
743743

744744
// If a search pattern is specified, load the posts that match
745745
if (!empty($q['s'])) {
746-
$q['s'] = addslashes_gpc($q['s']);
747-
$search = ' AND (';
748-
$q['s'] = preg_replace('/, +/', ' ', $q['s']);
749-
$q['s'] = str_replace(',', ' ', $q['s']);
750-
$q['s'] = str_replace('"', ' ', $q['s']);
751-
$q['s'] = trim($q['s']);
752-
if ($q['exact']) {
753-
$n = '';
754-
} else {
755-
$n = '%';
746+
// added slashes screw with quote grouping when done early, so done later
747+
$q['s'] = stripslashes($q['s']);
748+
if ($q['sentence']) {
749+
$q['search_terms'] = array($q['s']);
756750
}
757-
if (!$q['sentence']) {
758-
$s_array = explode(' ',$q['s']);
759-
$q['search_terms'] = $s_array;
760-
$search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
761-
for ( $i = 1; $i < count($s_array); $i = $i + 1) {
762-
$search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
763-
}
764-
$search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')';
765-
$search .= ')';
766-
} else {
767-
$search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))';
751+
else {
752+
preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches);
753+
$q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
754+
}
755+
$n = ($q['exact']) ? '' : '%';
756+
$searchand = '';
757+
foreach((array)$q['search_terms'] as $term) {
758+
$term = addslashes_gpc($term);
759+
$search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))";
760+
$searchand = ' AND ';
768761
}
762+
$term = addslashes_gpc($q['s']);
763+
if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] ) $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')";
764+
765+
$search = " AND ({$search}) ";
769766
}
770767

771768
// Category stuff

0 commit comments

Comments
 (0)