→ facetwp_sort_options

Overview

Customize the sorting logic, as well as the <select> options

To change the Sort box appearance, use the facetwp_sort_html filter.

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', 
);

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 );