Skip to content

Instantly share code, notes, and snippets.

@nickcernis
Forked from cdils/gist:5430275
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcernis/9c8d96623f7645e75dac to your computer and use it in GitHub Desktop.
Save nickcernis/9c8d96623f7645e75dac 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 ( 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' );
}
}
@nickcernis
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment