Woocommerce get products

前端 未结 4 619
抹茶落季
抹茶落季 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:17

    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.

提交回复
热议问题