X Features Cars Demo Power Plants Demo State Parks Demo Site Showcase Docs Tutorials Add-ons Buy



This filter lets you control the display order of facet values.

How it Works

FacetWP uses the facetwp_index table to store facet values. When loading values for a facet, the SQL query looks like the following:

SELECT * FROM wp_facetwp_index f WHERE {where} ORDER BY {orderby};

This filter lets you customize the {orderby} above. You have access to the table columns (facet_value, facet_display_value, term_id, etc.), but not menu_order. If you need to sort by arbitrary values, see the example below.


$facet = array(
    'name' => 'my_facet',
    'type' => 'checkboxes',
    'source' => 'tax/category',


Sort the label numerically (in ascending order)

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'years_old' == $facet['name'] ) {
        $orderby = 'f.facet_display_value+0 ASC';
    return $orderby;
}, 10, 2 );

Sort by arbitrary values

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'day_of_week' == $facet['name'] ) {
        // to sort by raw value, use "f.facet_value" instead
        $orderby = 'FIELD(f.facet_display_value, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")';
    return $orderby;
}, 10, 2 );