Allow excluded keywords when searching posts.

Pass a keyword with a leading hyphen to exclude posts containing that keyword.
For example, 'taco -onions' will return posts that contain the word 'taco' but
do not contain the word 'onions'.

Props akibjorklund.
Fixes #33988.
Built from https://develop.svn.wordpress.org/trunk@34934


git-svn-id: http://core.svn.wordpress.org/trunk@34899 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-10-08 03:18:24 +00:00
parent 7b230cafab
commit c2a0d593ac
2 changed files with 32 additions and 8 deletions

View File

@ -1509,7 +1509,8 @@ class WP_Query {
* @since 1.5.0
* @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
* array key to `$orderby`.
* @since 4.4.0 Introduced `$post_name__in` and `$title` parameters.
* @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded
* search terms, by prepending a hyphen.
* @access public
*
* @param string|array $query {
@ -1587,7 +1588,9 @@ class WP_Query {
* @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
* 'posts_per_page' when is_archive(), or is_search() are true.
* @type array $post_name__in An array of post slugs that results must match.
* @type string $s Search keyword.
* @type string $s Search keyword(s). Prepending a term with a hyphen will
* exclude posts matching that term. Eg, 'pillow -sofa' will
* return posts containing 'pillow' but not 'sofa'.
* @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
* @type bool $sentence Whether to search by phrase. Default false.
* @type bool $suppress_filters Whether to suppress filters. Default false.
@ -2158,13 +2161,24 @@ class WP_Query {
$searchand = '';
$q['search_orderby_title'] = array();
foreach ( $q['search_terms'] as $term ) {
if ( $n ) {
// Terms prefixed with '-' should be excluded.
$include = '-' !== substr( $term, 0, 1 );
if ( $include ) {
$like_op = 'LIKE';
$andor_op = 'OR';
} else {
$like_op = 'NOT LIKE';
$andor_op = 'AND';
$term = substr( $term, 1 );
}
if ( $n && $include ) {
$like = '%' . $wpdb->esc_like( $term ) . '%';
$q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
}
$like = $n . $wpdb->esc_like( $term ) . $n;
$search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like );
$search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like );
$searchand = ' AND ';
}
@ -2264,11 +2278,19 @@ class WP_Query {
if ( $q['search_terms_count'] > 1 ) {
$num_terms = count( $q['search_orderby_title'] );
$like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
// If the search terms contain negative queries, don't bother ordering by sentence matches.
$like = '';
if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
$like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
}
$search_orderby = '(CASE ';
// sentence match in 'post_title'
$search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
if ( $like ) {
$search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
}
// sanity limit, sort as sentence when more than 6 terms
// (few searches are longer than 6 terms and most titles are not)
@ -2281,7 +2303,9 @@ class WP_Query {
}
// sentence match in 'post_content'
$search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
if ( $like ) {
$search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
}
$search_orderby .= 'ELSE 5 END)';
} else {
// single word or sentence search

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.4-alpha-34933';
$wp_version = '4.4-alpha-34934';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.