I tried to use the above code adapted for a woocommerce sort and it did not work. It just sorts by the default. I am using the FacetWP plugin. Here is my code. It seems almost as if it cannot find the post_views field at all. I did add ‘post_type’ => ‘product’ to the query_args as well, and that did not work. I would appreciate your help getting this working.
function my_facetwp_sort_options( $options, $params ) {
$options[‘popularity_new’] = array(
‘label’ => ‘Popularity’,
‘query_args’ => array(
‘orderby’ => ‘post_views’,
‘order’ => ‘DESC’, // descending order
)
);
$options[‘price_high_to_low’] = array(
‘label’ => ‘Price: High to Low’,
‘query_args’ => array(
‘orderby’ => ‘meta_value_num’, // sort by numerical custom field
‘meta_key’ => ‘_price’, // required when sorting by custom fields
‘order’ => ‘DESC’, // descending order
)
);
$options[‘price_low_to_high’] = array(
‘label’ => ‘Price: Low to High’,
‘query_args’ => array(
‘orderby’ => ‘meta_value_num’, // sort by numerical custom field
‘meta_key’ => ‘_price’, // required when sorting by custom fields
‘order’ => ‘ASC’, // ascending order
)
);
return $options;
}
add_filter( ‘facetwp_sort_options’, ‘my_facetwp_sort_options’, 10, 2 );
I am using WordPress 4.3.1 and Woocommerce 2.4.7.