-
-
Save cdils/5430275 to your computer and use it in GitHub Desktop.
Change the sort order for an archive template by a custom field.
This file contains hidden or 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 | |
/** | |
* 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