Automattic\WooCommerce\Internal\ProductAttributesLookup

Filterer::get_product_counts_query_not_using_lookup_table()privateWC 1.0

Get the query for counting products by terms NOT using the product attributes lookup table.

Method of the class: Filterer{}

No Hooks.

Return

Array. An array of SQL query parts.

Usage

// private - for code of main (parent) class only
$result = $this->get_product_counts_query_not_using_lookup_table( $tax_query, $meta_query, $term_ids );
$tax_query(\WP_Tax_Query) (required)
The current main tax query.
$meta_query(\WP_Meta_Query) (required)
The current main meta query.
$term_ids(string) (required)
The term ids to include in the search.

Filterer::get_product_counts_query_not_using_lookup_table() code WC 8.6.1

private function get_product_counts_query_not_using_lookup_table( $tax_query, $meta_query, $term_ids ) {
	global $wpdb;

	$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
	$tax_query_sql  = $tax_query->get_sql( $wpdb->posts, 'ID' );

	// Generate query.
	$query           = array();
	$query['select'] = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) AS term_count, terms.term_id AS term_count_id";
	$query['from']   = "FROM {$wpdb->posts}";
	$query['join']   = "
		INNER JOIN {$wpdb->term_relationships} AS term_relationships ON {$wpdb->posts}.ID = term_relationships.object_id
		INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id )
		INNER JOIN {$wpdb->terms} AS terms USING( term_id )
		" . $tax_query_sql['join'] . $meta_query_sql['join'];

	$term_ids_sql   = $this->get_term_ids_sql( $term_ids );
	$query['where'] = "
		WHERE {$wpdb->posts}.post_type IN ( 'product' )
		AND {$wpdb->posts}.post_status = 'publish'
		{$tax_query_sql['where']} {$meta_query_sql['where']}
		AND terms.term_id IN $term_ids_sql";

	$search_query_sql = \WC_Query::get_main_search_query_sql();
	if ( $search_query_sql ) {
		$query['where'] .= ' AND ' . $search_query_sql;
	}

	$query['group_by'] = 'GROUP BY terms.term_id';

	return $query;
}