Automattic\WooCommerce\Internal\ProductFilters
FilterData::get_cached_product_ids
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.
Method of the class: FilterData{}
No Hooks.
Returns
String. Comma-separated list of product IDs.
Usage
// private - for code of main (parent) class only $result = $this->get_cached_product_ids( $query_vars );
- $query_vars(array) (required)
- The WP_Query arguments.
FilterData::get_cached_product_ids() FilterData::get cached product ids code WC 10.3.3
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;
}