Overview

This filter lets you customize the display order of choices within a facet.

How it works

FacetWP uses the facetwp_index table to store facet values. When loading values for a facet, the SQL query looks like the following:

SELECT * FROM wp_facetwp_index f WHERE {where} ORDER BY {orderby};

This filter lets you dynamically customize the {orderby} above. You have access to the table columns (facet_value, facet_display_value, term_id, etc.), but not menu_order. If you need to sort by arbitrary values, see the example below.

Parameters

  • $orderby | string | The original “ORDER BY”
  • $facet | array | An array of facet properties (see below)

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

$facet = [ 'name' => 'my_facet', 'type' => 'checkboxes', 'source' => 'tax/category', // ... ];

Switch the default ASC/DESC order

Many facet types have a “Sort by” setting which determines the order of the facet choices. For example, a Checkboxes facet has these four sort options: Highest count, Display value, Raw value, and Term order.

The ascending (ASC) or descending (DESCorder) of these default options is hard-coded. With this hook you can easily switch this order, by setting the desired order with the $orderby variable.

To do this, you must first know what the current $orderby value is. This depends on the order set in the “Sort by” setting, and on other settings of the facet. The easiest way to determine the current order, is to display it with print( $orderby ), as follows:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) { if ( 'my_facet_name' == $facet['name'] ) { // Change 'my_facet_name' to the name of your facet. Add more facet names to the array if needed. print( $orderby ); // Prints the current order to the bottom of the page. Comment out or remove this line when done. } return $orderby; }, 10, 2 );

This will output the current/default order to the bottom of the page displaying the facet. You can then copy this output, adapt it, and pass the desired order into $orderby variable.

Here is an example: if you have a Checkboxes facet with its “Sort by” setting set to order by “Display value” and its “Hierarchical” setting enabled, the print( $orderby ) in the above snippet will output this SQL to the bottom of the page:

f.depth, f.facet_display_value ASC

This means: order by hierarchical depth level first, then by facet_display_value, in ASC (A-Z) order.

Say you now want to switch this order to DESC (Z-A), you only have to adapt this order and pass it into the $orderby variable as follows:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) { if ( 'my_facet_name' == $facet['name'] ) { // Change 'my_facet_name' to the name of your facet. Add more facet names to the array if needed. // print( $orderby ); // Prints the current order to the bottom of the page. Comment out or remove this line when done. $orderby = 'f.depth, f.facet_display_value DESC'; // Switches the default ASC order to DESC in a hierarchical facet set to order by "Display value". } return $orderby; }, 10, 2 );

These are the default hard-coded facet $orderby values you’ll encounter depending on the facet settings:

/* Sort by Highest count */ counter DESC, f.facet_display_value ASC /* Sort by Highest count - Hierarchical */ f.depth, counter DESC, f.facet_display_value ASC /* Sort by Display value */ f.facet_display_value ASC /* Sort by Display value - Hierarchical */ f.depth, f.facet_display_value ASC /* Sort by Raw value */ f.facet_value ASC /* Sort by Raw value - Hierarchical */ f.depth, f.facet_value ASC /* Sort by Term order (example term ids) */ f.depth, FIELD(f.term_id, 185,186,205,149,65,70,49,58,62,38) /* Sort by Term order - Hierarchical (example term ids) */ f.depth, FIELD(f.term_id, 185,186,205,149,65,70,49,58,62,38)

Sort numerically

If your facet’s data source is a custom field containing numbers, and you want to sort the facet choices numerically, you can sort by facet_value (the raw/technical facet value as seen in the URL when selected) and add a +0 to it, which will (try to) convert the values to number values.

The following example sorts two facets named number_of_bedrooms and number_of_guests numerically. Adapt the facet name(s) to your situation and add the code to your (child) theme’s functions.php:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) { if ( in_array( $facet['name'], [ 'number_of_bedrooms', 'number_of_guests' ] ) ) { // change 'number_of_bedrooms' and 'number_of_guests' to the name(s) of your facet(s). Add or remove facets from the array if needed. $orderby = 'f.facet_value+0 ASC'; // Order numerically by raw value, in ascending order. } return $orderby; }, 10, 2 );

To sort by facet label instead of raw value, replace facet_value with facet_display_value. To use a descending order, replace ASC with DESC.

Sort by arbitrary values

The following example sorts a facet named day_of_week by day order. Adapt the facet name and options to your situation and add the code to your (child) theme’s functions.php:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) { if ( 'day_of_week' == $facet['name'] ) { // Change 'day_of_week' to the name of your facet $orderby = 'FIELD(f.facet_display_value, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")'; // Adapt to your facet's options } return $orderby; }, 10, 2 );

More examples

See also