Woocommerce get products

前端 未结 4 618
抹茶落季
抹茶落季 2020-12-05 01:30

I used the following code to get the list of product categories form WooCommerce in my WordPress website:

 

        
4条回答
  •  执念已碎
    2020-12-05 02:36

    Always use tax_query to get posts/products from a particular category or any other taxonomy. You can also get the products using ID/slug of particular taxonomy...

    $the_query = new WP_Query( array(
        'post_type' => 'product',
        'tax_query' => array(
            array (
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'accessories',
            )
        ),
    ) );
    
    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        the_title(); echo "
    "; endwhile; wp_reset_postdata();

提交回复
热议问题