Skip to content

Instantly share code, notes, and snippets.

@craiggrella
Last active August 29, 2015 14:02
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 craiggrella/b45e792f35bd4de5f5b9 to your computer and use it in GitHub Desktop.
Save craiggrella/b45e792f35bd4de5f5b9 to your computer and use it in GitHub Desktop.
Change Post Sort Order by Meta Key in WordPress
// change sort order of posts based on meta
function change_order_for_post_archive( $query ) {
//only show future events and events in the last 24hours
if ( $query->is_main_query() && is_post_type_archive('attorney') ) {
$query->set( 'meta_key', 'ecpt_position' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'change_order_for_post_archive' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment