When you’re doing custom PHP work, this snippet will come handy. It’s a quick way to get a sublist of product IDs based on product meta criteria – in this case we’ll get a list of products that have “_downloadable” set to “yes” (which, translated in English, means they are “downloadable”).
Of course, you can edit this snippet to get any sublist of product IDs, for example in stock products, custom field value products, below/above sales number, and so on. Whatever is stored as a custom field can be used. Enjoy!
PHP Snippet: Get List of WooCommerce Downloadable Products
/** * @snippet Get WooCommerce Downloadable Products * @how-to Watch tutorial @ https://businessbloomer.com/?p=19055 * @author Rodolfo Melogli * @compatible WooCommerce 3.7 * @donate $9 https://businessbloomer.com/bloomer-armada/ */ // Get downloadable products $product_ids = get_posts( array( 'post_type' => 'product', 'numberposts' => -1, 'post_status' => 'publish', 'fields' => 'ids', 'meta_query' => array( array( 'key' => '_downloadable', 'value' => 'yes', 'compare' => '=', )), )); // Print array on screen print_r( $product_ids );