From 3499e6fd731640e7e403cc23fe56335064aba9b2 Mon Sep 17 00:00:00 2001 From: Remi Corson Date: Wed, 4 Sep 2013 11:17:13 +0200 Subject: [PATCH] Added woocommerce_get_featured_product_ids() to retrieve featured products IDs within an array --- includes/wc-product-functions.php | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 20d0ee83739..794c2e56905 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -172,6 +172,49 @@ function woocommerce_get_product_ids_on_sale() { return $product_ids_on_sale; } +/** + * Function that returns an array containing the IDs of the featured products. + * + * @since 2.1 + * @access public + * @return array + */ +function woocommerce_get_featured_product_ids() { + + // Load from cache + $featured_product_ids = get_transient( 'wc_featured_products' ); + + // Valid cache found + if ( false !== $featured_product_ids ) + return $featured_product_ids; + + $featured = get_posts( array( + 'post_type' => array( 'product', 'product_variation' ), + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'meta_query' => array( + array( + 'key' => '_visibility', + 'value' => array('catalog', 'visible'), + 'compare' => 'IN' + ), + array( + 'key' => '_featured', + 'value' => 'yes' + ) + ), + 'fields' => 'id=>parent' + ) ); + + $product_ids = array_keys( $featured ); + $parent_ids = array_values( $featured ); + $featured_product_ids = array_unique( array_merge( $product_ids, $parent_ids ) ); + + set_transient( 'wc_featured_products', $featured_product_ids ); + + return $featured_product_ids; +} + /** * woocommerce_get_product_terms function. *