Automattic\WooCommerce\Internal\ProductFilters

FilterData::get_cached_product_idsprivateWC 1.0

Get cached product IDs from query vars.

Executes a WP_Query with the given query vars and returns a comma-separated string of product IDs. Results are cached to avoid repeated database queries.

Метод класса: FilterData{}

Хуков нет.

Возвращает

Строку. Comma-separated list of product IDs.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->get_cached_product_ids( $query_vars );
$query_vars(массив) (обязательный)
The WP_Query arguments.

Код FilterData::get_cached_product_ids() WC 10.3.4

private function get_cached_product_ids( array $query_vars ) {
	$cache_key = WC_Cache_Helper::get_cache_prefix( CacheController::CACHE_GROUP ) . md5( wp_json_encode( $query_vars ) );
	$cache     = wp_cache_get( $cache_key );

	if ( $cache ) {
		return $cache;
	}

	add_filter( 'posts_clauses', array( $this->query_clauses, 'add_query_clauses' ), 10, 2 );
	add_filter( 'posts_pre_query', '__return_empty_array' );

	$query_vars['no_found_rows']  = true;
	$query_vars['posts_per_page'] = -1;
	$query_vars['fields']         = 'ids';
	$query                        = new \WP_Query();

	$query->query( $query_vars );

	remove_filter( 'posts_clauses', array( $this->query_clauses, 'add_query_clauses' ), 10 );
	remove_filter( 'posts_pre_query', '__return_empty_array' );

	global $wpdb;

	// The query is already prepared by WP_Query.
	$results = $wpdb->get_results( $query->request, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

	if ( ! $results ) {
		$results = array();
	}

	$results = implode( ',', array_column( $results, 'ID' ) );

	wp_cache_set( $cache_key, $results );

	return $results;
}