Tell me how to replace the shortcode below with a similar code using WP_Query? The cycle I can build on the basis of the data obtained from the database. But how to set the correct database query to select products from the sale_products category or products from product_category?

  echo do_shortcode ('[product_category category = "computers"]');
 or 
 echo do_shortcode ('[sale_products per_page = "12"]');

    1 answer 1

    The cycle is, in general, the usual WordPress, given that the type of post - products , and not categories, but taxonomies. Product properties (for example, sales price) are contained in the meta-fields.

    For the test, an empty page was created using the template file. In the template file:

     <?php /* Template Name: woo-test-632707 */ echo '<h2>Товары категории "Фильмы"</h2>'; $args = array( 'post_type' => 'product', 'product_cat' => 'фильмы', 'posts_per_page' => -1 ); $wc_query = new WP_Query($args); if ($wc_query->have_posts()) { while ($wc_query->have_posts()) { $wc_query->the_post(); ?> <p><?php the_title(); ?></p> <?php } wp_reset_postdata(); } else { ?> <p><?php echo 'No Products'; ?></p> <?php } echo '<h2>Товары на распродаже</h2>'; $args = array( 'post_type' => 'product', 'meta_key' => '_sale_price', 'meta_value' => '0', 'meta_compare' => '>=', 'posts_per_page' => 12 ); $wc_query = new WP_Query($args); if ($wc_query->have_posts()) { while ($wc_query->have_posts()) { $wc_query->the_post(); ?> <p><?php the_title(); ?></p> <?php } wp_reset_postdata(); } else { ?> <p><?php echo 'No Products'; ?></p> <?php } ?> 

    The result is here .