{ return $product_visibility_terms[ 'rated-' . $rating ]; }, $parsed_filter_rating_values ); return array( 'tax_query' => array( array( 'field' => 'term_taxonomy_id', 'taxonomy' => 'product_visibility', 'terms' => $rating_terms, 'operator' => 'IN', 'rating_filter' => true, ), ), ); } /** * Return a query to filter products by taxonomies (product categories, product tags, etc.) * * For example: * User could provide "Product Categories" using "Filters" ToolsPanel available in Inspector Controls. * We use this function to extract it's query from $tax_query. * * For example, this is how the query for product categories will look like in $tax_query array: * Array * ( * [taxonomy] => product_cat * [terms] => Array * ( * [0] => 36 * ) * ) * * For product categories, taxonomy would be "product_tag" * * @param array $query WP_Query. * @return array Query to filter products by taxonomies. */ private function get_filter_by_taxonomies_query( $query ): array { if ( ! isset( $query['tax_query'] ) || ! is_array( $query['tax_query'] ) ) { return []; } $tax_query = $query['tax_query']; /** * Get an array of taxonomy names associated with the "product" post type because * we also want to include custom taxonomies associated with the "product" post type. */ $product_taxonomies = get_taxonomies( array( 'object_type' => array( 'product' ) ), 'names' ); $result = array_filter( $tax_query, function( $item ) use ( $product_taxonomies ) { return isset( $item['taxonomy'] ) && in_array( $item['taxonomy'], $product_taxonomies, true ); } ); return ! empty( $result ) ? [ 'tax_query' => $result ] : []; } /** * Returns the keyword filter from the given query. * * @param WP_Query $query The query to extract the keyword filter from. * @return array The keyword filter, or an empty array if none is found. */ private function get_filter_by_keyword_query( $query ): array { if ( ! is_array( $query ) ) { return []; } if ( isset( $query['s'] ) ) { return [ 's' => $query['s'] ]; } return []; } }