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 = array(
    'default' => array(
        'label' => __( 'Sort by', 'fwp' ),
        'query_args' => array()
    ),
    'title_asc' => array(
        'label' => __( 'Title (A-Z)', 'fwp' ),
        'query_args' => array(
            'orderby' => 'title',
            'order' => 'ASC',
        )
    ),
    'title_desc' => array(
        'label' => __( 'Title (Z-A)', 'fwp' ),
        'query_args' => array(
            'orderby' => 'title',
            'order' => 'DESC',
        )
    ),
    'date_desc' => array(
        'label' => __( 'Date (Newest)', 'fwp' ),
        'query_args' => array(
            'orderby' => 'date',
            'order' => 'DESC',
        )
    ),
    'date_asc' => array(
        'label' => __( 'Date (Oldest)', 'fwp' ),
        'query_args' => array(
            'orderby' => 'date',
            'order' => 'ASC',
        )
    )
);

The $params array provides the template name, if needed.

$params = array(
    '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

add_filter( 'facetwp_sort_options', function( $options, $params ) {
    $options['price_desc'] = array(
        'label' => 'Price (Highest)',
        'query_args' => array(
            'orderby' => 'meta_value_num',
            'meta_key' => 'edd_price',
            'order' => 'DESC',
        )
    );
    return $options;
}, 10, 2 );

More examples

See Also

facetwp_sort_html