facetwp_facet_sort_options
Overview
This hook lets you customize the Sort facet’s dropdown options and sort logic.
Parameters
- $options | array | Options array (see below)
- $params | array | Associative array of extra input variables (see below)
The $options
array contains an array of settings for each sort option that is set in the Sort facet’s settings. This example has two sort options:
$options = [
'title_asc' => [
'label' => 'Title (a-z)',
'query_args' => [
'orderby' => [
'title' => 'ASC'
]
]
],
'title_desc' => [
'label' => 'Title (z-a)',
'query_args' => [
'orderby' => [
'title' => 'DESC'
]
]
],
...
];
The $params
array provides the Sort facet’s settings, and the listing template’s name:
$params = [
'facet' => [
'name' => 'my_sort_facet_name',
'label' => 'My sort facet label',
'type' => 'sort',
'default_label' => 'Sort by',
'sort_options' => [
[
'label' => 'Title (a-z)',
'name' => 'title_a_z',
'orderby' => [
'key' => 'title',
'order' => 'ASC',
'type' => 'CHAR',
],
],
[
'label' => 'Title (z-a)',
'name' => 'title_z_a',
'orderby' => [
'key' => 'title',
'order' => 'DESC',
'type' => 'CHAR',
]
],
...
],
'operator' => 'or',
'selected_values' => [
'the_selected_sort_value'
]
],
'template_name' => 'my_template_name'
];
Usage
A few examples of how to use this hook:
Remove a sort option
The following code removes the sort option with the name title_z_a
:
add_filter( 'facetwp_facet_sort_options', function( $options, $params ) {
unset( $options['title_z_a'] );
return $options;
}, 10, 2 );
Remove a sort option conditionally
The following code removes the sort option with the name date
, if the template name is cars
:
add_filter( 'facetwp_facet_sort_options', function( $options, $params ) {
if ( 'cars' == $params['template_name'] ) {
unset( $options['date'] );
}
return $options;
}, 10, 2 );
Or, alternatively, use the Sort facet’s name as condition:
add_filter( 'facetwp_facet_sort_options', function( $options, $params ) {
if ( 'my_sort_facet' == $params['facet']['name'] ) {
unset( $options['date'] );
}
return $options;
}, 10, 2 );
Add a sort option conditionally
The following code adds a sort option if the template name is my_listing
. You can use any orderby parameter accepted by WP_Query. In this example we add an option to sort by post type:
add_filter( 'facetwp_facet_sort_options', function( $options, $params ) {
if ( 'my_listing' == $params['template_name'] ) {
$options['post_type'] = [
'label' => 'Post type',
'query_args' => [
'orderby' => [
'type' => 'ASC'
]
]
];
}
return $options;
}, 10, 2 );
Add an option to sort by multiple (custom) fields
The following example adds a “Price (Highest)” sort option. First it sorts by a custom field with the name _price
, from high to low. This custom field is a numeric field with decimals.
The new option also has a backup sorting method. Items with the same price will be sorted by post title:
add_filter( 'facetwp_facet_sort_options', function( $options, $params ) {
$options['price_desc'] = [
'label' => 'Price (Highest)',
'query_args' => [
'meta_query' => [
'sort_0' => [
'key' => '_price',
'type' => 'DECIMAL(16,4)',
]
],
'orderby'=> [
'sort_0' => 'DESC',
'title' => 'ASC'
]
]
];
return $options;
}, 10, 2 );
This above added sort option is equivalent to adding it like this in the Sort facet’s settings:
