Skip to content

Instantly share code, notes, and snippets.

@spyrosvl
Created November 6, 2017 13:02
Show Gist options
  • Save spyrosvl/94d98f67f0f73c9987df9ab6f6d243d6 to your computer and use it in GitHub Desktop.
Save spyrosvl/94d98f67f0f73c9987df9ab6f6d243d6 to your computer and use it in GitHub Desktop.
<?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