<?php

add_filter( 'facetwp_sort_options', function( $options, $params ) {
    $options['on_sale'] = array(
        'label' => 'On Sale',
        'query_args' => array(
            'sort_on_sale' => true,
            'meta_query' => array(
                array(
                    'key' => '_tilbud' // on sale
                ),
                array(
                    'key' => '_nypris' // price
                )
            )
        )
    );

    $options['price_asc'] = array(
        'label' => 'Price (lowest)',
        'query_args' => array(
            'meta_key' => '_nypris',
            'orderby' => 'meta_value_num',
            'order' => 'ASC'
         )
    );
    return $options;
}, 10, 2 );


// Sort by on sale (_tilbud), then by price (_nypris) ASC
add_filter( 'posts_orderby', function( $orderby, $wp_query ) {
    if ( isset( $wp_query->query_vars['sort_on_sale'] ) ) {
      $orderby = 'mt1.meta_value ASC, mt2.meta_value+0 ASC';
    }
    return $orderby;
}, 10, 2 );