phpwordpresssortingzerofacetwp

Sort price in FacetWP


In FacetWP Plugin (for Wordpress) I want to sort my results after 'price', so I added a new custom filter, described in their documentation. Currently, the sort results are looking like this:

  1. 4.450 €
  2. 399 €
  3. 3.990 €

I think, the code doesn't recognizes the last zero value. This is my code:

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

Already tried the "usort" function and the alternate 'price_raw_short' value (delivered by mobile.de) with no effect.


Solution

  • Do you have Woocommerce? Then you need to tell it that it is a number. Also the meta_key is _price

    Example for both ascending and Descending sorting:

    $options['price'] = array(
        'label' => __( 'Price: low to high', 'woocommerce' ),
        'query_args' => array(
            'orderby' => 'meta_value_num',
            'meta_key' => '_price',
            'order' => 'asc',
        )
    );
    
    $options['price-desc'] = array(
        'label' => __( 'Price: high to low', 'woocommerce' ),
        'query_args' => array(
            'orderby' => 'meta_value_num',
            'meta_key' => '_price',
            'order' => 'desc',
        )
    );