Title: WP_Query::parse_search_order
Published: April 25, 2014
Last modified: February 24, 2026

---

# WP_Query::parse_search_order( array $query_vars ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#wp--skip-link--target)

Generates SQL for the ORDER BY condition based on passed search terms.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#parameters)󠁿

 `$query_vars`arrayrequired

Query variables.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#return)󠁿

 string ORDER BY clause.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#source)󠁿

    ```php
    protected function parse_search_order( &$query_vars ) {
    	global $wpdb;

    	if ( $query_vars['search_terms_count'] > 1 ) {
    		$num_terms = count( $query_vars['search_orderby_title'] );

    		// If the search terms contain negative queries, don't bother ordering by sentence matches.
    		$like = '';
    		if ( ! preg_match( '/(?:\s|^)\-/', $query_vars['s'] ) ) {
    			$like = '%' . $wpdb->esc_like( $query_vars['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 ', $query_vars['search_orderby_title'] ) . ' THEN 2 ';
    			// Any word in title, not needed when $num_terms == 1.
    			if ( $num_terms > 1 ) {
    				$search_orderby .= 'WHEN ' . implode( ' OR ', $query_vars['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( $query_vars['search_orderby_title'] ) . ' DESC';
    	}

    	return $search_orderby;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-query.php#L1629)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-query.php#L1629-L1676)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#related)󠁿

| Uses | Description | 
| [wpdb::esc_like()](https://developer.wordpress.org/reference/classes/wpdb/esc_like/)`wp-includes/class-wpdb.php` |

First half of escaping for `LIKE` special characters `%` and `_` before preparing for SQL.

  | 
| [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)`wp-includes/class-wpdb.php` |

Prepares a SQL query for safe execution.

  |

| Used by | Description | 
| [WP_Query::get_posts()](https://developer.wordpress.org/reference/classes/wp_query/get_posts/)`wp-includes/class-wp-query.php` |

Retrieves an array of posts based on query variables.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_query/parse_search_order/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.7.0](https://developer.wordpress.org/reference/since/3.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_query%2Fparse_search_order%2F)
before being able to contribute a note or feedback.