diff --git a/includes/admin/list-tables/class-wc-admin-list-table-products.php b/includes/admin/list-tables/class-wc-admin-list-table-products.php index 943e537b2b8..3c50d73d141 100644 --- a/includes/admin/list-tables/class-wc-admin-list-table-products.php +++ b/includes/admin/list-tables/class-wc-admin-list-table-products.php @@ -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; diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index d83a44c43d8..713c0003a71 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -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,11 +1324,15 @@ 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( + $search_results = $wpdb->get_results( // phpcs:disable $wpdb->prepare( - "SELECT DISTINCT posts.ID FROM {$wpdb->posts} posts + "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id $type_join WHERE ( @@ -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, @@ -1347,6 +1353,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da // phpcs:enable ); + $product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) ); + if ( is_numeric( $term ) ) { $post_id = absint( $term ); $post_type = get_post_type( $post_id );