Products: Group API search items by id

This commit is contained in:
Paul Sealock 2019-03-19 16:36:47 +13:00
parent 36a60c59d5
commit 0aef533d6a
2 changed files with 21 additions and 19 deletions

View File

@ -64,28 +64,11 @@ class WC_Admin_REST_Product_Variations_Controller extends WC_REST_Product_Variat
public function get_items( $request ) {
add_filter( 'posts_where', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_product_search_filter' ), 10, 2 );
add_filter( 'posts_join', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_product_search_join' ), 10, 2 );
add_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_product_search_group_by' ), 10, 2 );
add_filter( 'posts_groupby', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_product_search_group_by' ), 10, 2 );
$response = parent::get_items( $request );
remove_filter( 'posts_where', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_product_search_filter' ), 10 );
remove_filter( 'posts_join', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_product_search_join' ), 10 );
remove_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_product_search_group_by' ), 10 );
remove_filter( 'posts_groupby', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_product_search_group_by' ), 10 );
return $response;
}
/**
* Group by post ID to prevent duplicates.
*
* @param string $groupby Group by clause used to organize posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public function add_wp_query_product_search_group_by( $groupby, $wp_query ) {
global $wpdb;
$search = trim( $wp_query->get( 'search' ) );
if ( $search && empty( $groupby ) ) {
$groupby = $wpdb->posts . '.ID';
}
return $groupby;
}
}

View File

@ -91,9 +91,11 @@ class WC_Admin_REST_Products_Controller extends WC_REST_Products_Controller {
public function get_items( $request ) {
add_filter( 'posts_where', array( __CLASS__, 'add_wp_query_product_search_filter' ), 10, 2 );
add_filter( 'posts_join', array( __CLASS__, 'add_wp_query_product_search_join' ), 10, 2 );
add_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_product_search_group_by' ), 10, 2 );
$response = parent::get_items( $request );
remove_filter( 'posts_where', array( __CLASS__, 'add_wp_query_product_search_filter' ), 10 );
remove_filter( 'posts_join', array( __CLASS__, 'add_wp_query_product_search_join' ), 10 );
remove_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_product_search_group_by' ), 10 );
return $response;
}
@ -136,4 +138,21 @@ class WC_Admin_REST_Products_Controller extends WC_REST_Products_Controller {
return $join;
}
/**
* Group by post ID to prevent duplicates.
*
* @param string $groupby Group by clause used to organize posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public function add_wp_query_product_search_group_by( $groupby, $wp_query ) {
global $wpdb;
$search = trim( $wp_query->get( 'search' ) );
if ( $search && empty( $groupby ) ) {
$groupby = $wpdb->posts . '.ID';
}
return $groupby;
}
}