Skip to content

Instantly share code, notes, and snippets.

@maor
Created September 30, 2012 01:11
Show Gist options
  • Save maor/3805555 to your computer and use it in GitHub Desktop.
Save maor/3805555 to your computer and use it in GitHub Desktop.
<?php
/* [http://on.fb.me/UAe3H9] */
/*
* Posts that have the 'mc_order_key' custom field, and that their (numeric) value is low
* will be shown first. If you wish to reverse the order, change the 'order' parameter to
* 'DESC'. Also make sure to change the meta key.
*/
function mc_reorder_search_results( $query ) {
/* Let's make sure we're on the search page, and running on the main query. */
if ( $query->is_search() && $query->is_main_query() ) {
$query->query_vars = array_merge( $query->query_vars, array(
'order' => 'ASC', /* Deafult is DESC, set for ASC to reverse. */
'orderby' => 'meta_value',
'meta_key' => 'mc_order_key' /* Change this key to match yours! */
) );
}
}
add_action( 'pre_get_posts', 'mc_reorder_search_results' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment