2017-06-13 94 views
0

我需要做一個自定義查詢返回所有產品適合我的查詢與他們的所有圖像,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; 
} 

任何幫助嗎?

回答

0

我覺得代碼的頂部是好的,我想下面的代碼更改爲類似:

$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;