Menu Close

WooCommerce: Get Product IDs (All, by Tag, by Category)

default

When you work with WooCommerce PHP snippets, you often need to “get” stuff off your WordPress database in order to make some calculations and return the result on the screen. And if there is something I often google is “How do I get all my store’s product IDs?“.

Thankfully the wc_get_products WooCommerce function gives us the answer. Enjoy 🙂

WooCommerce: get all product IDs

PHP Snippet 1: Get All WooCommerce Product IDs

/**
 * @snippet       Get All WooCommerce Product IDs
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

$args = array(
    'limit' => -1,
    'status' => 'publish',
    'return' => 'ids',
);
$products = wc_get_products( $args );

PHP Snippet 2: Get All WooCommerce Product IDs by Product Tag

/**
 * @snippet       Get WooCommerce Product IDs by Tag
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

$args = array(
    'limit' => -1,
    'status' => 'publish',
    'return' => 'ids',
    'tag' => array( 'tall' ),
);
$products = wc_get_products( $args );

PHP Snippet 3: Get All WooCommerce Product IDs by Product Category

/**
 * @snippet       Get WooCommerce Product IDs by Category
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

$args = array(
    'limit' => -1,
    'status' => 'publish',
    'return' => 'ids',
    'category' => array( 'books' ),
);
$products = wc_get_products( $args );
View Source
Posted in WooCommerce Tips