Skip to content

Instantly share code, notes, and snippets.

@robneu
Forked from cdils/gist:5430275
Created June 20, 2013 19:51
Show Gist options
  • Save robneu/5826042 to your computer and use it in GitHub Desktop.
Save robneu/5826042 to your computer and use it in GitHub Desktop.
<?php
/**
* 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
*
*/
add_action( 'pre_get_posts', 'cd_listing_sort_order' );
function cd_listing_sort_order( $query ) {
if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'listing' ) ) {
$price = (int)'_listing_price'; //convert prince to integer
$price = $price*1; //multiply by one
$query->set( 'meta_key', $price );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'desc' ); //list high to low
$query->set( 'posts_per_page', '6' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment