WooCommerce Code Reference

WC_Query
in package
Uses AccessiblePrivateMethods

WC_Query Class.

Table of Contents

$query_vars  : array<string|int, mixed>
Query vars to add to wp.
$chosen_attributes  : array<string|int, mixed>
Stores chosen attributes.
$filterer  : Filterer
The instance of the class that helps filtering with the product attributes lookup table.
$product_query  : WP_Query
Reference to the main product query on the page.
__construct()  : mixed
Constructor for the query class. Hooks in methods.
add_endpoints()  : mixed
Add endpoints for query vars.
add_query_vars()  : array<string|int, mixed>
Add query vars.
adjust_posts_count()  : int
This function used to be hooked to found_posts and adjust the posts count when the filtering by attribute widget was used and variable products were present. Now it isn't hooked anymore and does nothing but return the input unchanged, since the pull request in which it was introduced has been reverted.
get_catalog_ordering_args()  : array<string|int, mixed>
Returns an array of arguments for ordering products based on the selected values.
get_current_endpoint()  : string
Get query current active query var.
get_endpoint_title()  : string
Get page title for an endpoint.
get_endpoints_mask()  : int
Endpoint mask describing the places the endpoint should be added.
get_errors()  : mixed
Get any errors from querystring.
get_layered_nav_chosen_attributes()  : array<string|int, mixed>
Get an array of attributes and terms selected with the layered nav widget.
get_main_meta_query()  : array<string|int, mixed>
Get the meta query which was used by the main query.
get_main_query()  : WP_Query
Get the main query which product queries ran against.
get_main_search_query_sql()  : mixed
Based on WP_Query::parse_search
get_main_tax_query()  : array<string|int, mixed>
Get the tax query which was used by the main query.
get_meta_query()  : array<string|int, mixed>
Appends meta queries to an array.
get_products_in_view()  : mixed
Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients.
get_query_vars()  : array<string|int, mixed>
Get query vars.
get_tax_query()  : array<string|int, mixed>
Appends tax queries to an array.
handle_get_posts()  : array<string|int, mixed>
Handler for the 'the_posts' WP filter.
init_query_vars()  : mixed
Init query vars by loading options.
layered_nav_init()  : mixed
Layered nav init.
layered_nav_query()  : mixed
Layered Nav post filter.
order_by_popularity_post_clauses()  : array<string|int, mixed>
WP Core does not let us change the sort direction for individual orderby params - https://core.trac.wordpress.org/ticket/17065.
order_by_price_asc_post_clauses()  : array<string|int, mixed>
Handle numeric price sorting.
order_by_price_desc_post_clauses()  : array<string|int, mixed>
Handle numeric price sorting.
order_by_rating_post_clauses()  : array<string|int, mixed>
Order by rating post clauses.
parse_request()  : mixed
Parse the request and look for query vars - endpoints may not be supported.
pre_get_posts()  : mixed
Hook into pre_get_posts to do the main product query.
price_filter_post_clauses()  : array<string|int, mixed>
Custom query used to filter products by price.
product_query()  : mixed
Query the products, applying sorting/ordering etc.
rating_filter_meta_query()  : array<string|int, mixed>
Return a meta query for filtering by rating.
remove_add_to_cart_pagination()  : string
Remove the add-to-cart param from pagination urls.
remove_ordering_args()  : mixed
Remove ordering queries.
remove_posts_where()  : mixed
Remove the posts_where filter.
remove_product_query()  : mixed
Remove the query.
remove_product_query_filters()  : array<string|int, mixed>
Pre_get_posts above may adjust the main query to add WooCommerce logic. When this query is done, we need to ensure all custom filters are removed.
reset_chosen_attributes()  : mixed
Reset the chosen attributes so that get_layered_nav_chosen_attributes will get them from the query again.
search_post_excerpt()  : mixed
Search post excerpt.
stock_status_meta_query()  : array<string|int, mixed>
Returns a meta query to handle product stock status.
visibility_meta_query()  : array<string|int, mixed>
Returns a meta query to handle product visibility.
wpseo_metadesc()  : string
WP SEO meta description.
wpseo_metakey()  : string
WP SEO meta key.
get_current_posts()  : array<string|int, mixed>
Get the posts (or the ids of the posts) found in the current WP loop.
get_layered_nav_chosen_attributes_inst()  : array<string|int, mixed>
Instance version of get_layered_nav_chosen_attributes, needed for unit tests.
append_product_sorting_table_join()  : string
Join wc_product_meta_lookup to posts if not already joined.
filter_out_valid_front_page_query_vars()  : array<string|int, mixed>
Returns a copy of `$query` with all query vars that are allowed on the front page stripped.
is_query_var_valid_on_front_page()  : bool
Checks whether a query var is allowed on the front page or not.
is_showing_page_on_front()  : bool
Are we currently on the front page?
page_on_front_is()  : bool
Is the front page a page we define?
product_query_post_clauses()  : array<string|int, mixed>
Add extra clauses to the product query.

Properties

$chosen_attributes

Stores chosen attributes.

private static array<string|int, mixed> $chosen_attributes

$filterer

The instance of the class that helps filtering with the product attributes lookup table.

private Filterer $filterer

$product_query

Reference to the main product query on the page.

private static WP_Query $product_query

Methods

__construct()

Constructor for the query class. Hooks in methods.

public __construct() : mixed
Return values
mixed

add_endpoints()

Add endpoints for query vars.

public add_endpoints() : mixed
Return values
mixed

add_query_vars()

Add query vars.

public add_query_vars(array<string|int, mixed> $vars) : array<string|int, mixed>
Parameters
$vars : array<string|int, mixed>

Query vars.

Return values
array<string|int, mixed>

adjust_posts_count()

This function used to be hooked to found_posts and adjust the posts count when the filtering by attribute widget was used and variable products were present. Now it isn't hooked anymore and does nothing but return the input unchanged, since the pull request in which it was introduced has been reverted.

public adjust_posts_count(int $count, WP_Query $query) : int
Parameters
$count : int

Original posts count, as supplied by the found_posts filter.

$query : WP_Query

The current WP_Query object.

Tags
since
4.4.0
Return values
intAdjusted posts count.

get_catalog_ordering_args()

Returns an array of arguments for ordering products based on the selected values.

public get_catalog_ordering_args([string $orderby = '' ][, string $order = '' ]) : array<string|int, mixed>
Parameters
$orderby : string = ''

Order by param.

$order : string = ''

Order param.

Return values
array<string|int, mixed>

get_current_endpoint()

Get query current active query var.

public get_current_endpoint() : string
Return values
string

get_endpoint_title()

Get page title for an endpoint.

public get_endpoint_title(string $endpoint[, string $action = '' ]) : string
Parameters
$endpoint : string

Endpoint key.

$action : string = ''

Optional action or variation within the endpoint.

Tags
since
2.3.0
since
4.6.0

Added $action parameter.

Return values
stringThe page title.

get_endpoints_mask()

Endpoint mask describing the places the endpoint should be added.

public get_endpoints_mask() : int
Tags
since
2.6.2
Return values
int

get_layered_nav_chosen_attributes()

Get an array of attributes and terms selected with the layered nav widget.

public static get_layered_nav_chosen_attributes() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_main_meta_query()

Get the meta query which was used by the main query.

public static get_main_meta_query() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_main_query()

Get the main query which product queries ran against.

public static get_main_query() : WP_Query
Return values
WP_Query

get_main_search_query_sql()

Based on WP_Query::parse_search

public static get_main_search_query_sql() : mixed
Return values
mixed

get_main_tax_query()

Get the tax query which was used by the main query.

public static get_main_tax_query() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_meta_query()

Appends meta queries to an array.

public get_meta_query([array<string|int, mixed> $meta_query = array() ][, bool $main_query = false ]) : array<string|int, mixed>
Parameters
$meta_query : array<string|int, mixed> = array()

Meta query.

$main_query : bool = false

If is main query.

Return values
array<string|int, mixed>

get_products_in_view()

Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients.

public get_products_in_view() : mixed
Tags
deprecated
2.6.0

due to performance concerns

Return values
mixed

get_query_vars()

Get query vars.

public get_query_vars() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_tax_query()

Appends tax queries to an array.

public get_tax_query([array<string|int, mixed> $tax_query = array() ][, bool $main_query = false ]) : array<string|int, mixed>
Parameters
$tax_query : array<string|int, mixed> = array()

Tax query.

$main_query : bool = false

If is main query.

Return values
array<string|int, mixed>

handle_get_posts()

Handler for the 'the_posts' WP filter.

public handle_get_posts(array<string|int, mixed> $posts, WP_Query $query) : array<string|int, mixed>
Parameters
$posts : array<string|int, mixed>

Posts from WP Query.

$query : WP_Query

Current query.

Return values
array<string|int, mixed>

init_query_vars()

Init query vars by loading options.

public init_query_vars() : mixed
Return values
mixed

layered_nav_init()

Layered nav init.

public layered_nav_init() : mixed
Tags
deprecated
2.6.0
Return values
mixed

layered_nav_query()

Layered Nav post filter.

public layered_nav_query(mixed $deprecated) : mixed
Parameters
$deprecated : mixed

Deprecated.

Tags
deprecated
2.6.0

due to performance concerns

Return values
mixed

order_by_popularity_post_clauses()

WP Core does not let us change the sort direction for individual orderby params - https://core.trac.wordpress.org/ticket/17065.

public order_by_popularity_post_clauses(array<string|int, mixed> $args) : array<string|int, mixed>

This lets us sort by meta value desc, and have a second orderby param.

Parameters
$args : array<string|int, mixed>

Query args.

Return values
array<string|int, mixed>

order_by_price_asc_post_clauses()

Handle numeric price sorting.

public order_by_price_asc_post_clauses(array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

Query args.

Return values
array<string|int, mixed>

order_by_price_desc_post_clauses()

Handle numeric price sorting.

public order_by_price_desc_post_clauses(array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

Query args.

Return values
array<string|int, mixed>

order_by_rating_post_clauses()

Order by rating post clauses.

public order_by_rating_post_clauses(array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

Query args.

Return values
array<string|int, mixed>

parse_request()

Parse the request and look for query vars - endpoints may not be supported.

public parse_request() : mixed
Return values
mixed

pre_get_posts()

Hook into pre_get_posts to do the main product query.

public pre_get_posts(WP_Query $q) : mixed
Parameters
$q : WP_Query

Query instance.

Return values
mixed

price_filter_post_clauses()

Custom query used to filter products by price.

public price_filter_post_clauses(array<string|int, mixed> $args, WP_Query $wp_query) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

Query args.

$wp_query : WP_Query

WP_Query object.

Tags
since
3.6.0
Return values
array<string|int, mixed>

product_query()

Query the products, applying sorting/ordering etc.

public product_query(WP_Query $q) : mixed

This applies to the main WordPress loop.

Parameters
$q : WP_Query

Query instance.

Return values
mixed

rating_filter_meta_query()

Return a meta query for filtering by rating.

public rating_filter_meta_query() : array<string|int, mixed>
Tags
deprecated
3.0.0

Replaced with taxonomy.

Return values
array<string|int, mixed>

remove_add_to_cart_pagination()

Remove the add-to-cart param from pagination urls.

public remove_add_to_cart_pagination(string $url) : string
Parameters
$url : string

URL.

Return values
string

remove_ordering_args()

Remove ordering queries.

public remove_ordering_args() : mixed
Return values
mixed

remove_posts_where()

Remove the posts_where filter.

public remove_posts_where() : mixed
Tags
deprecated
3.2.0
  • Nothing to remove anymore because search_post_excerpt() is deprecated.
Return values
mixed

remove_product_query()

Remove the query.

public remove_product_query() : mixed
Return values
mixed

remove_product_query_filters()

Pre_get_posts above may adjust the main query to add WooCommerce logic. When this query is done, we need to ensure all custom filters are removed.

public remove_product_query_filters(array<string|int, mixed> $posts) : array<string|int, mixed>

This is done here during the_posts filter. The input is not changed.

Parameters
$posts : array<string|int, mixed>

Posts from WP Query.

Return values
array<string|int, mixed>

reset_chosen_attributes()

Reset the chosen attributes so that get_layered_nav_chosen_attributes will get them from the query again.

public static reset_chosen_attributes() : mixed
Return values
mixed

search_post_excerpt()

Search post excerpt.

public search_post_excerpt([string $where = '' ]) : mixed
Parameters
$where : string = ''

Where clause.

Tags
deprecated
3.2.0
  • Not needed anymore since WordPress 4.5.
Return values
mixed

stock_status_meta_query()

Returns a meta query to handle product stock status.

public stock_status_meta_query([string $status = 'instock' ]) : array<string|int, mixed>
Parameters
$status : string = 'instock'

(default: 'instock').

Tags
deprecated
3.0.0

Replaced with taxonomy.

Return values
array<string|int, mixed>

visibility_meta_query()

Returns a meta query to handle product visibility.

public visibility_meta_query([string $compare = 'IN' ]) : array<string|int, mixed>
Parameters
$compare : string = 'IN'

(default: 'IN').

Tags
deprecated
3.0.0

Replaced with taxonomy.

Return values
array<string|int, mixed>

wpseo_metadesc()

WP SEO meta description.

public wpseo_metadesc() : string

Hooked into wpseo_ hook already, so no need for function_exist.

Return values
string

wpseo_metakey()

WP SEO meta key.

public wpseo_metakey() : string

Hooked into wpseo_ hook already, so no need for function_exist.

Return values
string

get_current_posts()

Get the posts (or the ids of the posts) found in the current WP loop.

protected get_current_posts() : array<string|int, mixed>
Return values
array<string|int, mixed>Array of posts or post ids.

get_layered_nav_chosen_attributes_inst()

Instance version of get_layered_nav_chosen_attributes, needed for unit tests.

protected get_layered_nav_chosen_attributes_inst() : array<string|int, mixed>
Return values
array<string|int, mixed>

append_product_sorting_table_join()

Join wc_product_meta_lookup to posts if not already joined.

private append_product_sorting_table_join(string $sql) : string
Parameters
$sql : string

SQL join.

Return values
string

filter_out_valid_front_page_query_vars()

Returns a copy of `$query` with all query vars that are allowed on the front page stripped.

private filter_out_valid_front_page_query_vars(array<string|int, mixed> $query) : array<string|int, mixed>

Used when the shop page is also the front page.

Parameters
$query : array<string|int, mixed>

The unfiltered array.

Return values
array<string|int, mixed>The filtered query vars.

is_query_var_valid_on_front_page()

Checks whether a query var is allowed on the front page or not.

private is_query_var_valid_on_front_page(string $query_var) : bool
Parameters
$query_var : string

Query var name.

Return values
boolTRUE when query var is allowed on the front page. FALSE otherwise.

is_showing_page_on_front()

Are we currently on the front page?

private is_showing_page_on_front(WP_Query $q) : bool
Parameters
$q : WP_Query

Query instance.

Return values
bool

page_on_front_is()

Is the front page a page we define?

private page_on_front_is(int $page_id) : bool
Parameters
$page_id : int

Page ID.

Return values
bool

product_query_post_clauses()

Add extra clauses to the product query.

private product_query_post_clauses(array<string|int, mixed> $args, WP_Query $wp_query) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

Product query clauses.

$wp_query : WP_Query

The current product query.

Return values
array<string|int, mixed>The updated product query clauses array.