Automattic\WooCommerce\Internal\Admin\Reports
ProductSearchQuery::get_ids_subquery
Returns a SELECT statement resolving the given search terms to product IDs.
The statement yields a single product_id column, for use as a derived table or in an IN (...) clause.
Метод класса: ProductSearchQuery{}
Хуков нет.
Возвращает
string. SQL statement, or an empty string when there is nothing to search for.
Использование
$result = ProductSearchQuery::get_ids_subquery( $terms, $restrict_to_ids );
- $terms(строка|string[]) (обязательный)
- Search terms. A product matches if it matches any term.
- $restrict_to_ids(int[])
- Product IDs to intersect the results with. An ID that cannot belong to a product matches nothing.
По умолчанию:array()
Список изменений
| С версии 11.2.0 | Введена. |
Код ProductSearchQuery::get_ids_subquery() ProductSearchQuery::get ids subquery WC 11.2.2
public static function get_ids_subquery( $terms, $restrict_to_ids = array() ) {
$terms = self::parse_terms( $terms );
if ( empty( $terms ) ) {
return '';
}
$args = array(
'post_type' => 'product',
// Matches the search box, and a drafted product can still have sales to report.
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids',
// The report orders and pages the result itself, so the subquery does not have to.
'orderby' => 'none',
'no_found_rows' => true,
// The query is never run, so its empty result is not worth caching.
'cache_results' => false,
self::TERMS_QUERY_VAR => $terms,
);
$restrict_to_ids = (array) $restrict_to_ids;
if ( ! empty( $restrict_to_ids ) ) {
// WP_Query runs `post__in` through `absint()`, which would read the `-1` the report
// filters use for an empty set as product ID 1. Clamp to 0, which matches nothing.
$args['post__in'] = array_map( static fn( $id ) => max( 0, (int) $id ), $restrict_to_ids );
}
$statement = self::build_statement( $args );
// A plugin filtering `posts_fields` can add columns, so name the one this returns.
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- WP_Query prepares the statement it builds.
return "SELECT DISTINCT ID AS product_id FROM ( {$statement} ) AS wc_analytics_product_search_results";
}