Reputation: 563
I need to get of all products sizes(width and height). I use this code:
$allProducts = wc_get_products(array(
'status' => 'publish',
));
$allWidthArr = array();
$allHeightArr = array();
foreach ( $allProducts as $product ) {
$allWidthArr[] = $product->get_width();
$allHeightArr[] = $product->get_height();
}
Can I get this data without loop all products?
Upvotes: 1
Views: 104
Reputation: 329
I would recommend using a MYSQL Query.
Try the following lines of code,
global $wpdb;
$list_of_heights = $wpdb->get_results('SELECT post_meta.meta_value FROM '.$wpdb->prefix.'posts posts INNER JOIN '.$wpdb->prefix.'postmeta post_meta
ON post_meta.post_id=posts.ID where post_type="product" and post_meta.meta_key="_height"',ARRAY_N);
$list_of_widths = $wpdb->get_results('SELECT post_meta.meta_value FROM '.$wpdb->prefix.'posts posts INNER JOIN '.$wpdb->prefix.'postmeta post_meta
ON post_meta.post_id=posts.ID where post_type="product" and post_meta.meta_key="_width"',ARRAY_N);
Upvotes: 2