-
-
Save robneu/5937747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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