Tell me how to replace this shortcode with a similar code using WP_Query?

  echo do_shortcode ('[best_selling_products per_page = "12"]'); 

I did this:

  $ args = array (
     'post_type' => 'product',
     'posts_per_page' => 12,
     'meta_key' => 'total_sales',
     'orderby' => 'meta_value_num',
 );
 $ wc_query = new WP_Query ($ args);
 if ($ wc_query-> have_posts ()) {
 while ($ wc_query-> have_posts ()) {
     $ wc_query-> the_post ();

     woocommerce_product_loop_start ();
         wc_get_template_part ('content', 'product');
     woocommerce_product_loop_end ();

     }
 }
 wp_reset_postdata ();

but the fact is that the store is still in development, and the most popular products are already displayed as a result of this code. After all, the goods have not yet been sold. Do I display the best-selling products correctly?

    1 answer 1

    The code is correct. It displays product type records of 12 per page, sorting them by the total_sales meta-field. While there are no sales, for all products this field is 0.

    The value of the total_sales meta-field can be total_sales in a loop after wc_get_template_parts , for example:

     echo get_post_meta(get_the_ID(), 'total_sales')[0]; 

    To display products that have more than the specified number of sales (in the example> 0), you can use meta_query . Two loop options below.

     echo '<h2>Π‘Π°ΠΌΡ‹Π΅ ΠΏΡ€ΠΎΠ΄Π°Π²Π°Π΅ΠΌΡ‹Π΅ Ρ†ΠΈΠΊΠ»ΠΎΠΌ woocommerce</h2>'; $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => 12, 'meta_key' => 'total_sales', 'orderby' => 'meta_value_num', ); $wc_query = new WP_Query($args); if ($wc_query->have_posts()) { while ($wc_query->have_posts()) { $wc_query->the_post(); woocommerce_product_loop_start(); wc_get_template_part( 'content', 'product' ); echo 'Кол-Π²ΠΎ ΠΏΡ€ΠΎΠ΄Π°ΠΆ: ' . get_post_meta(get_the_ID(), 'total_sales')[0]; woocommerce_product_loop_end(); } } wp_reset_postdata(); echo '<h2>Π‘Π°ΠΌΡ‹Π΅ ΠΏΡ€ΠΎΠ΄Π°Π²Π°Π΅ΠΌΡ‹Π΅ Ρ†ΠΈΠΊΠ»ΠΎΠΌ woocommerce с ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ΠΌ</h2>'; $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => 12, 'orderby' => 'meta_value_num', 'meta_query' => array( array( // Simple products type 'key' => 'total_sales', 'value' => 0, 'compare' => '>', 'type' => 'numeric' ), ), ); $wc_query = new WP_Query($args); if ($wc_query->have_posts()) { while ($wc_query->have_posts()) { $wc_query->the_post(); woocommerce_product_loop_start(); wc_get_template_part( 'content', 'product' ); echo 'Кол-Π²ΠΎ ΠΏΡ€ΠΎΠ΄Π°ΠΆ: ' . get_post_meta(get_the_ID(), 'total_sales')[0]; woocommerce_product_loop_end(); } } wp_reset_postdata(); 

    The test result is here .

    • > Since there was no sales, all products in this field are 0. Sorting is not performed yet. - word
    • Then the counter question: how then to make sure that the goods are not withdrawn until they are popular? - word
    • the counter question to the counter question)) - and what does it mean - popular? criterion? - KAGG Design
    • I understand that best_selling_products means the best-selling products. Or is it better not to bother much with popular products like for a new store? How to make sure that there are no best-selling goods so that goods are not derived from this taxonomy? For earlier, very grateful for the tips. - word
    • best_selling is not a taxonomy. This is a sample by database, sorted by the "quantity of sales" meta-field. While there are no sales, you can not display at all - KAGG Design