Skip to content

Instantly share code, notes, and snippets.

@spyrosvl
Last active November 6, 2017 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spyrosvl/2f1b1163337194c8ed35e9bfcf9e0670 to your computer and use it in GitHub Desktop.
Save spyrosvl/2f1b1163337194c8ed35e9bfcf9e0670 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 non-zero priced post IDs
$sql_non_zero = "
SELECT post_id
FROM {$wpdb->prefix}postmeta
WHERE meta_key = 'price' AND meta_value > 0 AND post_id IN (" . implode( ',', $post_ids ) . ") ORDER BY CAST(meta_value AS int) ASC";
$results_non_zero = $wpdb->get_col( $sql_non_zero );
$sql_zero = "
SELECT post_id
FROM {$wpdb->prefix}postmeta
WHERE meta_key = 'price' AND meta_value = 0 AND post_id IN (" . implode( ',', $post_ids ) . ") ORDER BY post_id";
$results_zero = $wpdb->get_col( $sql_zero );
// merge the 2 results
$post_ids = array_merge( $results_non_zero, $results_zero );
return $post_ids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment