简体   繁体   English

如何使用wp_query woocommerce获得最畅销的产品

[英]How to get best selling products using wp_query woocommerce

I want to get Best selling Products on Home page. 我想在首页上获得“最畅销产品”。 I am using Below query But this is not getting best selling products from all categories. 我正在使用以下查询,但是这并不是所有类别中最畅销的产品。

If I put best selling product short code that is working but custom structure could not get those products. 如果我将畅销的产品短代码放上去,但是自定义结构无法获得这些产品。 These My Query Function.. 这些我的查询功能

function get_product_posts_hp()
{
    wp_reset_query();
    $args = array(
        'posts_per_page' => 12,
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    );
    return new WP_Query($args);
}

Thanks In Advance..

Your code works globally… I have tried with the following function code: 您的代码在全球范围内均可使用...我尝试使用以下功能代码:

function get_product_posts_hp(){
    $query = new WP_Query( array(
        'posts_per_page' => 12,
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts' => 1,
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    ) );

    echo '<p>Count: '. $query->post_count ; '</p>';

    if($query->have_posts()) :
        while($query->have_posts()) : $query->the_post();

            echo '<p>' . get_the_title() . ' (';
            echo get_post_meta( get_the_id(), 'total_sales', true) . ')</p>';

        endwhile;
        wp_reset_postdata();
    endif;
}

And I get 12 products titles displayed by total sales DESC. 我得到了按销售总额DESC显示的12个产品标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM