If you want to change the order of an already existing query, you shouldn’t create a new WP_Query
object but instead use pre_get_posts
to alter the existing query, which prevents multiple queries running unnecessarily.
From the pre_get_posts
docs:
This hook is called after the query variable object is created, but before the actual query is run.
This is an example of how to change all queries of a custom post type to order by a custom field. Obviously it will need to be changed to fit your situation but should give you an idea of what to do:
/**
* Alters queries
*/
add_action( 'pre_get_posts', 'wpse_217090_pre_get_posts' );
function wpse_217090_pre_get_posts( $query ) {
// don't run on the backend
if ( is_admin() )
return;
// Only run on post types 'your_custom_post_type'
if ( is_post_type_archive('your_custom_post_type') && $query->is_main_query() || $post_type == 'your_custom_post_type' ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', '_your_custom_field' );
$query->set( 'order', 'ASC' );
}
return;
}
For reference you should read up on;