programing

wp_query woocmerce를 사용하여 베스트셀러 상품을 얻는 방법

codeshow 2023. 10. 9. 23:40
반응형

wp_query woocmerce를 사용하여 베스트셀러 상품을 얻는 방법

홈페이지에서 베스트셀러 제품을 구매하고 싶습니다.Below query를 사용하고 있습니다만, 모든 카테고리에서 베스트셀러 제품을 얻는 것은 아닙니다.

베스트셀링 상품 숏코드를 넣었는데 작동은 되는데 커스텀 구조로는 그 상품들을 받을 수가 없었습니다.이러한 내 쿼리 함수..

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..

당신의 코드는 전세계적으로 작동합니다.다음 기능 코드로 시도해 보았습니다.

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

그리고 총 매출 DESC별로 12개의 상품 타이틀이 표시됩니다.

언급URL : https://stackoverflow.com/questions/52217639/how-to-get-best-selling-products-using-wp-query-woocommerce

반응형