facetwp_sort_options
Overview
FacetWP provides a built-in “Sort by” box (see the Shortcodes section). This hook lets you customize its dropdown options and sort logic.
The above is the default sort box appearance, as seen on our Cars demo.
Parameters
- $options | array | Options array (see below)
- $params | array | Associative array of extra input variables (see below)
$options = [ 'default' => [ 'label' => __( 'Sort by', 'fwp' ), 'query_args' => [] ], 'title_asc' => [ 'label' => __( 'Title (A-Z)', 'fwp' ), 'query_args' => [ 'orderby' => 'title', 'order' => 'ASC', ] ], 'title_desc' => [ 'label' => __( 'Title (Z-A)', 'fwp' ), 'query_args' => [ 'orderby' => 'title', 'order' => 'DESC', ] ], 'date_desc' => [ 'label' => __( 'Date (Newest)', 'fwp' ), 'query_args' => [ 'orderby' => 'date', 'order' => 'DESC', ] ], 'date_asc' => [ 'label' => __( 'Date (Oldest)', 'fwp' ), 'query_args' => [ 'orderby' => 'date', 'order' => 'ASC', ] ] ];
The $params array provides the template name, if needed.
$params = [ 'template_name' => 'default', ];
Changing the default “Sort by”
In the above code, notice that the default sort’s query_args
is empty.
The “Sort by” option is only a placeholder. When the “Sort by” option is active, it falls back to the query’s default sort. To change the default sort, you’ll need to change the query itself.
Usage
Example 1: Change the sort label
add_filter( 'facetwp_sort_options', function( $options, $params ) { $options['default']['label'] = 'My sort label'; return $options; }, 10, 2 );
Example 2: Remove the “Date (Oldest)” sort option
add_filter( 'facetwp_sort_options', function( $options, $params ) { unset( $options['date_asc'] ); return $options; }, 10, 2 );
Example 3: Add a “Price (Highest)” sort option.
Note: use
_price
for WooCommerce, or edd_price
for Easy Digital Downloadsadd_filter( 'facetwp_sort_options', function( $options, $params ) { $options['price_desc'] = [ 'label' => 'Price (Highest)', 'query_args' => [ 'orderby' => 'meta_value_num', 'meta_key' => '_price', 'order' => 'DESC', ] ]; return $options; }, 10, 2 );