Improve product search and use WPDB instead of several get_posts queries
Fixes #9203 @claudiosmweb
This commit is contained in:
parent
81f787e13b
commit
786c87523f
|
@ -1783,6 +1783,8 @@ class WC_AJAX {
|
||||||
* @param string $post_types (default: array('product'))
|
* @param string $post_types (default: array('product'))
|
||||||
*/
|
*/
|
||||||
public static function json_search_products( $x = '', $post_types = array( 'product' ) ) {
|
public static function json_search_products( $x = '', $post_types = array( 'product' ) ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
check_ajax_referer( 'search-products', 'security' );
|
check_ajax_referer( 'search-products', 'security' );
|
||||||
|
@ -1794,80 +1796,42 @@ class WC_AJAX {
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! empty( $_GET['exclude'] ) ) {
|
$like_term = '%' . $wpdb->esc_like( $term ) . '%';
|
||||||
$exclude = array_map( 'intval', explode( ',', $_GET['exclude'] ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
$args = array(
|
|
||||||
'post_type' => $post_types,
|
|
||||||
'post_status' => 'publish',
|
|
||||||
'posts_per_page' => -1,
|
|
||||||
's' => $term,
|
|
||||||
'fields' => 'ids',
|
|
||||||
'exclude' => $exclude
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( is_numeric( $term ) ) {
|
if ( is_numeric( $term ) ) {
|
||||||
|
$query = $wpdb->prepare( "
|
||||||
if ( false === array_search( $term, $exclude ) ) {
|
SELECT ID FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id
|
||||||
$posts2 = get_posts( array(
|
WHERE posts.post_status = 'publish'
|
||||||
'post_type' => $post_types,
|
AND (
|
||||||
'post_status' => 'publish',
|
posts.post_parent = %s
|
||||||
'posts_per_page' => -1,
|
OR posts.ID = %s
|
||||||
'post__in' => array( 0, $term ),
|
OR posts.post_title LIKE %s
|
||||||
'fields' => 'ids'
|
OR (
|
||||||
) );
|
postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s
|
||||||
|
)
|
||||||
|
)
|
||||||
|
", $term, $term, $term, $like_term );
|
||||||
} else {
|
} else {
|
||||||
$posts2 = array();
|
$query = $wpdb->prepare( "
|
||||||
|
SELECT ID FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id
|
||||||
|
WHERE posts.post_status = 'publish'
|
||||||
|
AND (
|
||||||
|
posts.post_title LIKE %s
|
||||||
|
or posts.post_content LIKE %s
|
||||||
|
OR (
|
||||||
|
postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s
|
||||||
|
)
|
||||||
|
)
|
||||||
|
", $like_term, $like_term, $like_term );
|
||||||
}
|
}
|
||||||
|
|
||||||
$posts3 = get_posts( array(
|
$query .= " AND posts.post_type IN ('" . implode( "','", array_map( 'esc_sql', $post_types ) ) . "')";
|
||||||
'post_type' => $post_types,
|
|
||||||
'post_status' => 'publish',
|
|
||||||
'posts_per_page' => -1,
|
|
||||||
'post_parent' => $term,
|
|
||||||
'fields' => 'ids',
|
|
||||||
'exclude' => $exclude
|
|
||||||
) );
|
|
||||||
|
|
||||||
$posts4 = get_posts( array(
|
|
||||||
'post_type' => $post_types,
|
|
||||||
'post_status' => 'publish',
|
|
||||||
'posts_per_page' => -1,
|
|
||||||
'meta_query' => array(
|
|
||||||
array(
|
|
||||||
'key' => '_sku',
|
|
||||||
'value' => $term,
|
|
||||||
'compare' => 'LIKE'
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'fields' => 'ids',
|
|
||||||
'exclude' => $exclude
|
|
||||||
) );
|
|
||||||
|
|
||||||
$posts = array_unique( array_merge( get_posts( $args ), $posts2, $posts3, $posts4 ) );
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
$args2 = array(
|
|
||||||
'post_type' => $post_types,
|
|
||||||
'post_status' => 'publish',
|
|
||||||
'posts_per_page' => -1,
|
|
||||||
'meta_query' => array(
|
|
||||||
array(
|
|
||||||
'key' => '_sku',
|
|
||||||
'value' => $term,
|
|
||||||
'compare' => 'LIKE'
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'fields' => 'ids',
|
|
||||||
'exclude' => $exclude
|
|
||||||
);
|
|
||||||
|
|
||||||
$posts = array_unique( array_merge( get_posts( $args ), get_posts( $args2 ) ) );
|
|
||||||
|
|
||||||
|
if ( ! empty( $_GET['exclude'] ) ) {
|
||||||
|
$query .= " AND posts.ID NOT IN (" . implode( ',', array_map( 'intval', explode( ',', $_GET['exclude'] ) ) ) . ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$posts = array_unique( $wpdb->get_col( $query ) );
|
||||||
$found_products = array();
|
$found_products = array();
|
||||||
|
|
||||||
if ( ! empty( $posts ) ) {
|
if ( ! empty( $posts ) ) {
|
||||||
|
|
Loading…
Reference in New Issue