Overview

FacetWP Sort facetThe Sort facet type adds a sort box for changing the listing order.

It might seem a bit counter-intuitive that Sort is a facet type, but just like any other facet type, a Sort facet can be added by making a new facet in the FacetWP admin interface.

Usage

Go to the Facets tab, click “add new”, choose “Sort” as Facet type, set its options, and place it on your page with a shortcode.

Available options

Name Description
Default label The dropdown label that the user sees by default (i.e. when no sort has been selected).
Sort options The UI for customizing the choices that appear in the sort dropdown. Each row corresponds with a sort choice, allowing you to set:

  • the label. Note: these labels are translatable with the facetwp_i18n hook.
  • the name (i.e. what gets added to the URL).
  • the sort criteria. Each criteria includes the field name and sort direction, and each row can actually contain multiple criteria. An example is if you wanted to add a fallback sort.

Sort options

The following example shows the setup for a sort box containing the default label of “Sort results” and 3 choices (Title DESC, Price DESC, and MPG Highest).

Sort by distance

When a Proximity facet is active, the post IDs are automatically sorted (nearest to furthest). This makes it possible to sort results by distance.

To enable sort by distance, create a new sort option and set its data source to post__in with ASC order.

If you want to sort the results by distance on page load, use this custom code.

Custom field formatting

For numerical sorts like “Price”, the custom field should contain a raw integer or decimal value (with a dot). Fields containing dollar signs, commas, or other symbols won’t work.

For date sorts, the field should contain values in YYYY-MM-DD, YYYYMMDD or YYYY-MM-DD HH:MM:SS format. The default “Post Date” and “Post Modified” fields use this format already. But if you’re using a custom field in some other format, it likely won’t work.

Sorting WooCommerce product variations

If you are using FacetWP with WooCommerce product variations, be aware that sorting by product variation prices is currently not (yet) supported with the Sort facet. The logic of the Sort facet is based on WP_Query and therefore supports only basic custom fields-based sorting. Product variation prices are unfortunately more complex than that.

If you need sorting by product variation price, you could use WooCommerce’s built-in sort box, which works on the “normal” WooCommerce product archive. FacetWP fully supports WooCommerce product archive pages.

See also