最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - get Woocommerce product format json for WP_Query

programmeradmin2浏览0评论

I'm building custom endpoint REST API and I need the same format wooCommerce gave for its post type ( product ), using the WP_Query() class.

I tried the wc_Product_Query and for some reason not working with my tax_query, So instead, I start using the Wordpress query WP_Query and works very well but the wooCommerce has much data than just default post like price, categories, images ...etc

So How I implement those data on my query?

My code:


function hash_realated_products() {

    $query = new WP_Query( array(

                                 'post_type'=> 'product',
                                 'per_page' => 10,
                                 'status' => 'publish',
                                 'orderby' => 'rand',
                                 'tax_query' => array(
                                                      'relation' => 'AND',
                                                      array(
                                                            'taxonomy' => 'product_tag',
                                                            'field'    => 'name',
                                                            'terms'    => array('component'),
                                                            'operator' => 'NOT IN',
                                                            'compare' => 'LIKE'
                                                            ),
                                                      array(
                                                            'taxonomy' => 'product_cat',
                                                            'field'    => 'term_id',
                                                            'terms'     =>  array(17), // When you have more term_id's seperate them by komma.
                                                            'operator'  => 'IN'
                                                        )
                                                      )
                                         ));
    $products = $query->get_posts();

    wp_reset_postdata();

    return $products;

}
发布评论

评论列表(0)

  1. 暂无评论