//= First let’s modify the sorting form
add_filter(‘cd_sort_filter’, ‘my_car_demon_sort’,10,1);
function my_car_demon_sort($car_demon_sorting) {
// $car_demon_sorting contains the current sorting content
//= Put everything on a single line
$car_demon_sorting = str_replace(chr(10),”,$car_demon_sorting);
$car_demon_sorting = str_replace(chr(11),”,$car_demon_sorting);
$car_demon_sorting = str_replace(chr(13),”,$car_demon_sorting);
//= Search for the sorting drop down
$start = ‘ tag and replace it with $new_option +
$drop_down_field = str_replace(‘‘, $new_option.’‘, $drop_down_field);
//= Now search $car_demon_sorting for the original drop down and replace it with our new one
$car_demon_sorting = str_replace($form_piece[0], $drop_down_field, $car_demon_sorting);
}
//= Return the modified sorting form
return $car_demon_sorting;
}
//= Now we need to modify the query
add_filter(‘cd_query_filter’, ‘my_car_query’,10,1);
function my_car_query($car_demon_query) {
// $car_demon_query contains the current query
//= Check to make sure order_by was set
if (isset($_GET[‘order_by’])) {
//= Sanitize it and set it to a variable
$order_by = sanitize_text_field($_GET[‘order_by’]);
//= Since _stock_value is an existing meta field we can set it like this
$car_demon_query[‘meta_key’] = $order_by;
}
return $car_demon_query;
}