Update get_count to use lookup table

This commit is contained in:
Mike Jolley 2019-05-09 10:38:26 +01:00
parent d7b81e40d4
commit a3b90cdb9e
1 changed files with 12 additions and 11 deletions

View File

@ -99,18 +99,19 @@ class WC_Admin_Reports_Stock_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
* @return int Count.
*/
private function get_count( $status ) {
$query_args = array();
$query_args['post_type'] = array( 'product', 'product_variation' );
$query_args['meta_query'] = array( // WPCS: slow query ok.
array(
'key' => '_stock_status',
'value' => $status,
),
);
global $wpdb;
$query = new WP_Query();
$query->query( $query_args );
return intval( $query->found_posts );
return (int) $wpdb->get_var(
$wpdb->prepare(
"
SELECT count( DISTINCT posts.ID ) FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
WHERE posts.post_type IN ( 'product', 'product_variation' )
AND wc_product_meta_lookup.stock_status = %s
",
$status
)
);
}
/**