返回从woocommerce过滤产品列表
问题描述:
我需要做一个自定义查询返回所有产品适合我的查询与他们的所有图像,meta和所有“高级自定义字段”acf为每个产品json格式。返回从woocommerce过滤产品列表
我部分成功使用下面的代码:
function get_woocommerce_product_list($request) {
$query = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_vendor',
'value' => 'farsi',
),
)
);
$result = [];
$wp_query = new WP_Query($query);
foreach ($wp_query->posts as $post) {
$result[$post->ID]['product'] = $post;
}
return $result;
}
任何帮助吗?
答
我觉得代码的顶部是好的,我想下面的代码更改为类似:
$wp_query = new WP_Query($query);
$result = array();
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) {
$wp_query->the_post();
$result[] = array(
'product_name' => get_the_title(),
'price' => get_post_meta(get_the_ID(), '_regular_price', true),
//find rest of values you require and put them into the array here
);
}
}
$json_result = json_encode($result);
return $json_result;