Adding a conditional in a hook

I have the function below which I would like to add a conditional to.

See ** Comment ** below in the code for what I’d like to do.

function my_facetwp_sort_options( $options, $params ) {
unset( $options['date_asc'] );
unset( $options['date_desc'] );
unset( $options['title_asc'] );
unset( $options['title_desc'] );

** if meta_key positions exists run the following ** 

$options['positions_asc']  = array(
        'label'      => 'Positions (Ascending)',
        'query_args' => array(
            'orderby'  => 'meta_value_num', // sort by numerical custom field
            'meta_key' => 'positions', // required when sorting by custom fields
            'order'    => 'ASC', // ascending order
        )
    );
$options['positions_desc'] = array(
    'label'      => 'Positions (Descending)',
    'query_args' => array(
        'orderby'  => 'meta_value_num', // sort by numerical custom field
        'meta_key' => 'positions', // required when sorting by custom fields
        'order'    => 'DESC', // descending order
    )
);

** end if here **

return $options;
}

add_filter( 'facetwp_sort_options', 'my_facetwp_sort_options', 10, 2 );

Read more here: Adding a conditional in a hook

Leave a Reply

Your email address will not be published. Required fields are marked *