How to output a list of specific products only in woocommerce?

Member

by addison , in category: PHP CMS , 7 months ago

How to output a list of specific products only in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , 7 months ago

@addison 

To output a list of specific products only in WooCommerce, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$args = array(
    'post_type' => 'product',
    'post__in' => array(1, 2, 3), // Replace these numbers with the IDs of the specific products you want to output
);

$products = new WP_Query($args);

if ($products->have_posts()) {
    while ($products->have_posts()) {
        $products->the_post();
        wc_get_template_part('content', 'product');
    }
}

wp_reset_postdata();


You can add this code to your theme's functions.php file or create a custom shortcode to output the list of specific products on your WooCommerce site. Just replace the array of post IDs with the IDs of the specific products you want to display.