我正在为 woocommerce 编写一个定价表插件。用户插入带有 woocommerce 产品类别 ID 的短代码,更新页面后,用户可以看到一个包含产品名称和价格列表的表格。
我怎样才能获得带有类别ID的产品列表?
在下面的代码中$pid是用户输入的简码,'对象id'是 wp_posts 表中每个产品的 id。
<?php
$products = $wpdb->get_results("SELECT object_id FROM {$wpdb->term_relationships}
WHERE term_taxonomy_id = " . $pid);
if(!empty($products))
{
foreach($products as $product)
{
//the code
}
}
?>
提前致谢。
$args = array(
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '[ category id here ]', // When you have more term_id's seperate them by komma.
'operator' => 'IN'
)
)
);
$the_query = wp_query($args);
未经测试但应该可以工作
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)