Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
add_filter( 'facetwp_filtered_post_ids', 'car_zero_priced_last', 10, 2);
function car_zero_priced_last( $post_ids, $class ) {
if (!isset($class->ajax_params['http_params']['get']['fwp_sort'])) {
return $post_ids;
}
if (isset($class->ajax_params['http_params']['get']['fwp_sort']) && $class->ajax_params['http_params']['get']['fwp_sort'] != 'price_asc') {
return $post_ids;
}
global $wpdb;
// Lookup zero priced post IDs
$sql = "
SELECT post_id
FROM {$wpdb->prefix}postmeta
WHERE meta_key = 'price' AND meta_value > '0' AND post_id IN (" . implode( ',', $post_ids ) . ")";
$results = $wpdb->get_col( $sql );
$diff = array_diff($post_ids, $results);
// default sort, zero priced last
$post_ids = array_merge($results, $diff );
return $post_ids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.