Query: Avoid invalid SQL when building ORDER BY clause using long search strings.

The introduction of negative search terms in 4.4 [34934] introduced the
possibility that the ORDER BY clause of a search query could be assembled in
such a way as to create invalid syntax. The current changeset fixes this by
ensuring that the ORDER BY clause corresponding to the search terms is
excluded when it would otherwise be empty.

Merges [36251] to the 4.4 branch.
Props salvoaranzulla, boonebgorges.
Fixes #35361.

Built from https://develop.svn.wordpress.org/branches/4.4@36354


git-svn-id: http://core.svn.wordpress.org/branches/4.4@36321 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dion Hulse 2016-01-20 04:38:27 +00:00
parent 7b2cd6dbd9
commit b2275a579b

View File

@ -2300,7 +2300,7 @@ class WP_Query {
$like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
}
$search_orderby = '(CASE ';
$search_orderby = '';
// sentence match in 'post_title'
if ( $like ) {
@ -2321,7 +2321,10 @@ class WP_Query {
if ( $like ) {
$search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
}
$search_orderby .= 'ELSE 5 END)';
if ( $search_orderby ) {
$search_orderby = '(CASE ' . $search_orderby . 'ELSE 5 END)';
}
} else {
// single word or sentence search
$search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';