Skip to content

Instantly share code, notes, and snippets.

@cdils
Forked from billerickson/gist:1535891
Last active December 16, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cdils/5430275 to your computer and use it in GitHub Desktop.
Save cdils/5430275 to your computer and use it in GitHub Desktop.
Change the sort order for an archive template by a custom field.
<?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