[woocommerce_product_filter_context]

This shortcode is used to establish a filter context as described below. It is not used to produce any particular output by itself.

  • The shortcode determines the current filter context based on the attributes given.
  • The shortcode must be placed within the page content. It will not work if it is placed in a widget or within a page builder’s block.
  • The shortcode must only be used once per individual page.
  • Combinations of taxonomies and terms can be used as detailed in the explanations provided for the attributes below.

The effect of the shortcode is equivalent to including the taxonomy and term context parameters of the [woocommerce_product_filter_products] shortcode. Using a separate shortcode instead can be clearer where the separation of context and rendering is more intuitive or simply more readable when you edit a page. It further allows to establish the context early within a page when needed, while the products and filter widgets can be placed independently.

Important: The shortcode must be placed within the page content. It will not work if it is placed in a widget or within a page builder’s block. Use the editor in plain Text mode to insert the shortcode and make sure that you use straight double quotes " to enclose the specified attributes.

Example – Custom Shop Page based on a Category ↑ Back to top

We want to create a dedicated shop page that is limited to products from a Tops product category. It should allow visitors to search by keyword within the page and update the results dynamically.

To achieve this, we place these shortcodes on a new page:

[woocommerce_product_filter_context taxonomy="product_cat" term="Tops"]
[woocommerce_product_filter]
[woocommerce_product_filter_products]
  • The first shortcode is the one discussed here, it establishes the product category Tops as the current filter context.
  • The shortcode on the second line simply adds a live search filter field on the page.
  • And the one on the third line renders the matching products in the fashion of a usual shop page. The products that it will display will be limited to those of the Tops category.

In this case we just include a live search filter field, but we can use any of the live filters (as widgets, shortcodes or API functions in the template).

This is an example of how the page renders when used with one of our test shops:

We can use the live filter field to search within those products:

Shortcode Attributes ↑ Back to top

The shortcode takes the following attributes to establish the context on a page:

  • taxonomy – One or more taxonomies separated by comma, used in combination with the term attribute. Any valid product taxonomy can be used, including product_cat for product category, product_tag for product tags; for product attributes, use the pa_ prefix of the related taxonomy, e.g. pa_size for a Size product attribute, pa_color for a Color product attribute, etc.
  • term – One or more terms can be given by name, slug or ID.

When different taxonomies are provided, include one for each term in the same order. In this way, you can choose a context where the products belong to any term of the first taxonomy while also relating to any term of the second taxonomy, etc.

Some further examples will help to clarify how these parameters are used …

– Include only products that are related to the Cars product category:

[woocommerce_product_filter_context taxonomy="product_cat" term="Cars"]

– Include products from the Pants or from the Shorts product categories:

[woocommerce_product_filter_context taxonomy="product_cat" term="Pants,Shorts"]

– Include products from the Dresses category that have colors red or orange related:

[woocommerce_product_filter_context taxonomy="product_cat,pa_color,pa_color" term="Dresses,red,orange"]

 

WooCommerce - the most customizable eCommerce platform for building your online business.

Back to the top