-
-
Save nickcernis/9c8d96623f7645e75dac to your computer and use it in GitHub Desktop.
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 ( is_admin() || ! $query->is_main_query() ) | |
return; | |
if ( is_post_type_archive( 'listing' ) || ( is_search() && get_query_var( 'post_type' ) == 'listing' ) ) { | |
$query->set( 'meta_key', '_listing_price' ); | |
$query->set( 'orderby', 'meta_value_num' ); | |
$query->set( 'order', 'desc' ); // High to low. Change to 'asc' to see low to high. | |
$query->set( 'posts_per_page', '6' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will work as long as prices are stored as numbers (500000) and not strings ($500,000). It should be possible to apply price formatting to listings with PHP/JS instead of storing prices as strings.