WooCommerce Code Reference

WC_Query
in package

WC_Query Class.

Table of Contents

$query_vars  : array
Query vars to add to wp.
$chosen_attributes  : array
Stores chosen attributes.
$product_query  : array
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
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
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
Get an array of attributes and terms selected with the layered nav widget.
get_main_meta_query()  : array
Get the meta query which was used by the main query.
get_main_query()  : array
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
Get the tax query which was used by the main query.
get_meta_query()  : array
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
Get query vars.
get_tax_query()  : array
Appends tax queries to an array.
handle_get_posts()  : array
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
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
Handle numeric price sorting.
order_by_price_desc_post_clauses()  : array
Handle numeric price sorting.
order_by_rating_post_clauses()  : array
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
Custom query used to filter products by price.
product_query()  : mixed
Query the products, applying sorting/ordering etc.
rating_filter_meta_query()  : array
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
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.
search_post_excerpt()  : mixed
Search post excerpt.
stock_status_meta_query()  : array
Returns a meta query to handle product stock status.
visibility_meta_query()  : array
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
Get the posts (or the ids of the posts) found in the current WP loop.
get_layered_nav_chosen_attributes_inst()  : array
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.
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?

Properties

Methods

__construct()

Constructor for the query class. Hooks in methods.

public __construct() : mixed
Return values
mixed

add_query_vars()

Add query vars.

public add_query_vars(array $vars) : array
Parameters
$vars : array

Query vars.

Return values
array

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
Parameters
$orderby : string = ''

Order by param.

$order : string = ''

Order param.

Return values
array

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
Parameters
$endpoint : string

Endpoint key.

Return values
string

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
Return values
array

get_main_meta_query()

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

public static get_main_meta_query() : array
Return values
array

get_main_query()

Get the main query which product queries ran against.

public static get_main_query() : array
Return values
array

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
Return values
array

get_meta_query()

Appends meta queries to an array.

public get_meta_query([array $meta_query = array() ][, bool $main_query = false ]) : array
Parameters
$meta_query : array = array()

Meta query.

$main_query : bool = false

If is main query.

Return values
array

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_tax_query()

Appends tax queries to an array.

public get_tax_query([array $tax_query = array() ][, bool $main_query = false ]) : array
Parameters
$tax_query : array = array()

Tax query.

$main_query : bool = false

If is main query.

Return values
array

handle_get_posts()

Handler for the 'the_posts' WP filter.

public handle_get_posts(array $posts, WP_Query $query) : array
Parameters
$posts : array

Posts from WP Query.

$query : WP_Query

Current query.

Return values
array

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 $args) : array

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

Parameters
$args : array

Query args.

Return values
array

order_by_price_asc_post_clauses()

Handle numeric price sorting.

public order_by_price_asc_post_clauses(array $args) : array
Parameters
$args : array

Query args.

Return values
array

order_by_price_desc_post_clauses()

Handle numeric price sorting.

public order_by_price_desc_post_clauses(array $args) : array
Parameters
$args : array

Query args.

Return values
array

order_by_rating_post_clauses()

Order by rating post clauses.

public order_by_rating_post_clauses(array $args) : array
Parameters
$args : array

Query args.

Return values
array

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 $args, WC_Query $wp_query) : array
Parameters
$args : array

Query args.

$wp_query : WC_Query

WC_Query object.

Tags
since
3.6.0
Return values
array

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
Tags
deprecated
3.0.0

Replaced with taxonomy.

Return values
array

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_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 $posts) : array

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

Parameters
$posts : array

Posts from WP Query.

Return values
array

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
Parameters
$status : string = 'instock'

(default: 'instock').

Tags
deprecated
3.0.0

Replaced with taxonomy.

Return values
array

visibility_meta_query()

Returns a meta query to handle product visibility.

public visibility_meta_query([string $compare = 'IN' ]) : array
Parameters
$compare : string = 'IN'

(default: 'IN').

Tags
deprecated
3.0.0

Replaced with taxonomy.

Return values
array

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
Return values
arrayArray 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
Return values
array

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

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