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

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

by Tutor Aspire

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 get_posts WordPress function gives us the answer. In the next weeks we’ll also see how we can get other information, such as all SKUs, all product categories, etc.

So, here’s the quick snippet to return all product IDs on the Cart page (“woocommerce_before_cart” hook) – you can change that if you want to use them somewhere else. Enjoy 🙂

WooCommerce: get all product IDs

PHP Snippet 1: Get All WooCommerce Product IDs

/**
 * @snippet       Get All WooCommerce Product IDs
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://www.tutoraspire.com
 */

add_action( 'woocommerce_before_cart', 'tutoraspire_echo_product_ids' );

function tutoraspire_echo_product_ids() {
   $all_ids = get_posts( array(
        'post_type' => 'product',
        'numberposts' => -1,
        'post_status' => 'publish',
        'fields' => 'ids',
   ) );
   foreach ( $all_ids as $id ) {
        echo $id;
   }
}

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

/**
 * @snippet       Get WooCommerce Product IDs by Tag
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://www.tutoraspire.com
 */

add_action( 'woocommerce_before_cart', 'tutoraspire_echo_product_ids_belong_to_tag' );

function tutoraspire_echo_product_ids_belong_to_tag() {
   $all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
         array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => 'your_product_tag',
            'operator' => 'IN',
         )
      ),
   ) );
   foreach ( $all_ids as $id ) {
      echo $id;
   }
}

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

/**
 * @snippet       Get WooCommerce Product IDs by Category
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://www.tutoraspire.com
 */

add_action( 'woocommerce_before_cart', 'tutoraspire_echo_product_ids_belong_to_cat' );

function tutoraspire_echo_product_ids_belong_to_cat() {
   $all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
         array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'your_product_cat',
            'operator' => 'IN',
         )
      ),
   ) );
   foreach ( $all_ids as $id ) {
      echo $id;
   }
}

You may also like