WC_Query::get_main_search_query_sql
Based on WP_Query::parse_search
Method of the class: WC_Query{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WC_Query::get_main_search_query_sql();
WC_Query::get_main_search_query_sql() WC Query::get main search query sql code WC 10.9.1
public static function get_main_search_query_sql() {
global $wpdb;
// PHPStan infers $product_query as non-nullable from other call sites. See https://github.com/woocommerce/woocommerce/pull/64360#issuecomment-4360066970.
// @phpstan-ignore-next-line isset.property See above.
if ( ! isset( self::$product_query ) ) {
return '';
}
$args = self::$product_query->query_vars;
$search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array();
$sql = array();
foreach ( $search_terms as $term ) {
// 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 );
}
$like = '%' . $wpdb->esc_like( $term ) . '%';
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$sql[] = $wpdb->prepare( "(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like );
}
if ( ! empty( $sql ) && ! is_user_logged_in() ) {
$sql[] = "($wpdb->posts.post_password = '')";
}
return implode( ' AND ', $sql );
}