![](/img/trans.png)
[英]How to get the product Id from a meta key and a meta value in WooCommerce
[英]How to get WooComrnce product category by meta key value
我有元值“old_id”的產品類別,我正在嘗試制作一個 function,它將使用舊ID(元鍵“old_id”)返回新產品類別 ID。
這是我試過的:
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false, // also retrieve terms which are not used yet
'meta_query' => array(
array(
'key' => 'old_id',
'value' => $old_cat_id,
'compare' => '='
)
),
)
);
使用 get_categories 不適用於 woocomrnce 類別。
然后我嘗試了另一種方式:
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'old_id',
'value' => $old_cat_id,
'compare' => '='
)
),
),
),
);
$query = new WP_Query($args);
由於某種原因,這個查詢根本不起作用,我錯過了什么嗎?
add_action('init', 'get_woocommerce_product_cats');
function get_woocommerce_product_cats() {
$orderby = 'name';
$order = 'asc';
$hide_empty = false;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'taxonomy' => 'product_cat',
'meta_key' => 'old_id',
'meta_value' => $old_cat_id
);
$product_categories = get_terms($cat_args);
echo '<pre>';
print_r($product_categories);
echo '</pre>';
exit;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.