Skip to content

Instantly share code, notes, and snippets.

@infoscigeek
Created April 8, 2017 07:43
Show Gist options
  • Save infoscigeek/95bddb6e97a2ee8aa841fa52ee1ade2b to your computer and use it in GitHub Desktop.
Save infoscigeek/95bddb6e97a2ee8aa841fa52ee1ade2b to your computer and use it in GitHub Desktop.
Order an archive page for a post category by a numeric custom field
<?php
/*Custom sort cateogry id 5 archive */
function isg_custom_property_archive( $query ) {
// only modify queries for category 5 if it exists
if ( !is_admin() && term_exists(5,'category') && $query->is_category(5)) {
$query->set('posts_per_page','-1');
$query->set('offset','0');
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'price');
$query->set('order', 'DESC');
}
}
add_filter( 'pre_get_posts', 'isg_custom_property_archive' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment