英文:
woocommerce list of all products sizes
问题
我需要获取所有产品的尺寸(宽度和高度)。我使用以下代码:
$allProducts = wc_get_products(array(
'status' => 'publish',
));
$allWidthArr = array();
$allHeightArr = array();
foreach ( $allProducts as $product ) {
$allWidthArr[] = $product->get_width();
$allHeightArr[] = $product->get_height();
}
我可以在不循环所有产品的情况下获取这些数据吗?
英文:
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?
答案1
得分: 2
我建议使用MYSQL查询。
尝试以下代码行,
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);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论