Woocommerce get products Woocommerce get products wordpress wordpress

Woocommerce get products


<?php      $args = array(        'post_type'      => 'product',        'posts_per_page' => 10,        'product_cat'    => 'hoodies'    );    $loop = new WP_Query( $args );    while ( $loop->have_posts() ) : $loop->the_post();        global $product;        echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';    endwhile;    wp_reset_query();?>

This will list all product thumbnails and names along with their links to product page. change the category name and posts_per_page as per your requirement.


Do not use WP_Query() or get_posts(). From the WooCommerce doc:

wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.

You can retrieve the products you want like this:

$args = array(    'category' => array( 'hoodies' ),    'orderby'  => 'name',);$products = wc_get_products( $args );

WooCommerce documentation

Note: the category argument takes an array of slugs, not IDs.


<?php$args     = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );$products = get_posts( $args ); ?>

This should grab all the products you want, I may have the post type wrong though I can't quite remember what woo-commerce uses for the post type. It will return an array of products