Skip to content

Instantly share code, notes, and snippets.

@robneu
Forked from cdils/gist:5430275
Last active December 19, 2015 10:08
Show Gist options
  • Save robneu/5937747 to your computer and use it in GitHub Desktop.
Save robneu/5937747 to your computer and use it in GitHub Desktop.
<?php
/************************************************
* This is not currently working.
************************************************/
add_action( 'pre_get_posts', 'cd_listing_sort_order' );
/**
* Change the post order for listings
*
* @author Carrie Dils
* @link http://www.billerickson.net/customize-the-wordpress-query/
* @reference http://codex.wordpress.org/Class_Reference/WP_Query
*
*/
function cd_listing_sort_order( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'listing' ) ) {
$raw_price = '_listing_price'; //convert prince to integer
// convert "," to "."
$raw_price = str_replace(',', '.', $raw_price);
// remove everything except numbers and dot "."
$raw_price = preg_replace("/[^0-9\.]/", "", $raw_price);
// remove all seperators from first part and keep the end
$raw_price = str_replace('.', '',substr($raw_price, 0, -3)) . substr($raw_price, -3);
$price = intval($raw_price);
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' ); //list high to low
$query->set( 'meta_key', $price );
$query->set( 'posts_per_page', '50' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment