WC_Product_Data_Store_CPT::get_featured_product_ids()publicWC 3.0.0

Returns a list of product IDs ( id as key => parent as value) that are featured. Uses get_posts instead of wc_get_products since we want some extra meta queries and ALL products (posts_per_page = -1).

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Return

Array.

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->get_featured_product_ids();

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::get_featured_product_ids() code WC 9.3.3

public function get_featured_product_ids() {
	$product_visibility_term_ids = wc_get_product_visibility_term_ids();

	return get_posts(
		array(
			'post_type'      => array( 'product', 'product_variation' ),
			'posts_per_page' => -1,
			'post_status'    => 'publish',
			'tax_query'      => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
				'relation' => 'AND',
				array(
					'taxonomy' => 'product_visibility',
					'field'    => 'term_taxonomy_id',
					'terms'    => array( $product_visibility_term_ids['featured'] ),
				),
				array(
					'taxonomy' => 'product_visibility',
					'field'    => 'term_taxonomy_id',
					'terms'    => array( $product_visibility_term_ids['exclude-from-catalog'] ),
					'operator' => 'NOT IN',
				),
			),
			'fields'         => 'id=>parent',
		)
	);
}