facetwp_sort_options
Overview
This hook lets you customize the old sort box’s dropdown options and sort logic.
Parameters
- $options | array | Options array (see below)
- $params | array | Associative array of extra input variables (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
$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.
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
$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
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_sort_options', function( $options, $params ) { $options['default']['label'] = 'My sort label'; return $options; }, 10, 2 );
Example 2: Remove the “Date (Oldest)” sort option
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_sort_options', function( $options, $params ) { unset( $options['date_asc'] ); return $options; }, 10, 2 );
Example 3: Remove the “Date (Oldest)” sort option only if the template is named cars
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_sort_options', function( $options, $params ) { if ( 'cars' == $params['template_name'] ) { unset( $options['date_asc'] ); } return $options; }, 10, 2 );
Example 4: Add a “Price (Highest)” sort option.
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_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 );