Uses
Uses | Description |
---|---|
wp-includes/wp-db.php: wpdb::esc_like() | First half of escaping for LIKE special characters % and _ before preparing for MySQL. |
wp-includes/wp-db.php: wpdb::prepare() | Prepares a SQL query for safe execution. |
Generates SQL for the ORDER BY condition based on passed search terms.
(array) (Required) Query variables.
(string) ORDER BY clause.
File: wp-includes/class-wp-query.php
protected function parse_search_order( &$q ) { global $wpdb; if ( $q['search_terms_count'] > 1 ) { $num_terms = count( $q['search_orderby_title'] ); // 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 = ''; // Sentence match in 'post_title'. 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). if ( $num_terms < 7 ) { // All words in title. $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 '; // Any word in title, not needed when $num_terms == 1. if ( $num_terms > 1 ) { $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 '; } } // Sentence match in 'post_content' and 'post_excerpt'. if ( $like ) { $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like ); $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like ); } if ( $search_orderby ) { $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)'; } } else { // Single word or sentence search. $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC'; } return $search_orderby; }
Version | Description |
---|---|
3.7.0 | Introduced. |
© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_query/parse_search_order