Added ability to search for all post statuses

This commit is contained in:
Mike Jolley 2018-02-08 12:53:39 +00:00
parent f5492cc50a
commit 7dd9c7fb0e
2 changed files with 9 additions and 3 deletions

View File

@ -459,7 +459,7 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
// Search using CRUD.
if ( isset( $query_vars['s'] ) ) {
$data_store = WC_Data_Store::load( 'product' );
$ids = $data_store->search_products( wc_clean( $query_vars['s'] ), '', true );
$ids = $data_store->search_products( wc_clean( $query_vars['s'] ), '', true, true );
$query_vars['post__in'] = array_merge( $ids, array( 0 ) );
// So we know we are searching products.
$query_vars['product_search'] = true;

View File

@ -1304,9 +1304,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
* @param string $term Search term.
* @param string $type Type of product.
* @param bool $include_variations Include variations in search or not.
* @param bool $all_statuses Should we search all statuses or limit to published?
* @return array of ids
*/
public function search_products( $term, $type = '', $include_variations = false ) {
public function search_products( $term, $type = '', $include_variations = false, $all_statuses = false ) {
global $wpdb;
$like_term = '%' . $wpdb->esc_like( $term ) . '%';
@ -1314,6 +1315,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$post_statuses = current_user_can( 'edit_private_products' ) ? array( 'private', 'publish' ) : array( 'publish' );
$type_join = '';
$type_where = '';
$status_where = '';
if ( $type ) {
if ( in_array( $type, array( 'virtual', 'downloadable' ), true ) ) {
@ -1322,6 +1324,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
}
}
if ( ! $all_statuses ) {
$status_where = " AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "') ";
}
// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
$product_ids = $wpdb->get_col(
// phpcs:disable
@ -1337,7 +1343,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
)
)
AND posts.post_type IN ('" . implode( "','", $post_types ) . "')
AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "')
$status_where
$type_where
ORDER BY posts.post_parent ASC, posts.post_title ASC",
$like_term,