diff --git src/wp-includes/meta.php src/wp-includes/meta.php
index 8953aaa..b03a40b 100644
--- src/wp-includes/meta.php
+++ src/wp-includes/meta.php
@@ -934,6 +934,14 @@ class WP_Meta_Query {
 	protected $table_aliases = array();
 
 	/**
+	 * A flat list of clauses, keyed by table aliases.
+	 *
+	 * @since 4.2.0
+	 * @var array
+	 */
+	protected $clauses = array();
+
+	/**
 	 * Constructor.
 	 *
 	 * @since 3.2.0
@@ -1374,6 +1382,14 @@ class WP_Meta_Query {
 		// Save the alias to this clause, for future siblings to find.
 		$clause['alias'] = $alias;
 
+		// Determine the data type.
+		$meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
+		$clause['cast'] = $meta_type;
+
+		// Store the clause in our flat array.
+		$clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
+		$this->clauses[ $clause_name ] =& $clause;
+
 		// Next, build the WHERE clause.
 
 		// meta_key.
@@ -1388,7 +1404,6 @@ class WP_Meta_Query {
 		// meta_value.
 		if ( array_key_exists( 'value', $clause ) ) {
 			$meta_value = $clause['value'];
-			$meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
 
 			if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
 				if ( ! is_array( $meta_value ) ) {
@@ -1450,6 +1465,10 @@ class WP_Meta_Query {
 		return $sql_chunks;
 	}
 
+	public function get_clauses() {
+		return $this->clauses;
+	}
+
 	/**
 	 * Identify an existing table alias that is compatible with the current
 	 * query clause.
diff --git src/wp-includes/query.php src/wp-includes/query.php
index 5a6774e..1725d2c 100644
--- src/wp-includes/query.php
+++ src/wp-includes/query.php
@@ -2233,8 +2233,9 @@ class WP_Query {
 
 		$primary_meta_key = '';
 		$primary_meta_query = false;
-		if ( ! empty( $this->meta_query->queries ) ) {
-			$primary_meta_query = reset( $this->meta_query->queries );
+		$meta_clauses = $this->meta_query->get_clauses();
+		if ( ! empty( $meta_clauses ) ) {
+			$primary_meta_query = reset( $meta_clauses );
 
 			if ( ! empty( $primary_meta_query['key'] ) ) {
 				$primary_meta_key = $primary_meta_query['key'];
@@ -2243,6 +2244,7 @@ class WP_Query {
 
 			$allowed_keys[] = 'meta_value';
 			$allowed_keys[] = 'meta_value_num';
+			$allowed_keys   = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
 		}
 
 		if ( ! in_array( $orderby, $allowed_keys ) ) {
@@ -2260,29 +2262,32 @@ class WP_Query {
 			case 'ID':
 			case 'menu_order':
 			case 'comment_count':
-				$orderby = "$wpdb->posts.{$orderby}";
+				$orderby_clause = "$wpdb->posts.{$orderby}";
 				break;
 			case 'rand':
-				$orderby = 'RAND()';
+				$orderby_clause = 'RAND()';
 				break;
 			case $primary_meta_key:
 			case 'meta_value':
 				if ( ! empty( $primary_meta_query['type'] ) ) {
-					$sql_type = $this->meta_query->get_cast_for_type( $primary_meta_query['type'] );
-					$orderby = "CAST($wpdb->postmeta.meta_value AS {$sql_type})";
+					$orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
 				} else {
-					$orderby = "$wpdb->postmeta.meta_value";
+					$orderby_clause = "{$primary_meta_query['alias']}.meta_value";
 				}
 				break;
 			case 'meta_value_num':
-				$orderby = "$wpdb->postmeta.meta_value+0";
+				$orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
+				break;
+			case array_key_exists( $orderby, $meta_clauses ):
+				$meta_clause = $meta_clauses[ $orderby ];
+				$orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
 				break;
 			default:
-				$orderby = "$wpdb->posts.post_" . $orderby;
+				$orderby_clause = "$wpdb->posts.post_" . $orderby;
 				break;
 		}
 
-		return $orderby;
+		return apply_filters( 'parse_orderby', $orderby_clause, $orderby );
 	}
 
 	/**
@@ -2813,6 +2818,12 @@ class WP_Query {
 
 		$where .= $search . $whichauthor . $whichmimetype;
 
+		if ( ! empty( $this->meta_query->queries ) ) {
+			$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
+			$join   .= $clauses['join'];
+			$where  .= $clauses['where'];
+		}
+
 		$rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
 		if ( ! isset( $q['order'] ) ) {
 			$q['order'] = $rand ? '' : 'DESC';
@@ -3030,12 +3041,6 @@ class WP_Query {
 			$where .= ')';
 		}
 
-		if ( !empty( $this->meta_query->queries ) ) {
-			$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
-			$join .= $clauses['join'];
-			$where .= $clauses['where'];
-		}
-
 		/*
 		 * Apply filters on where and join prior to paging so that any
 		 * manipulations to them are reflected in the paging by day queries.
diff --git tests/phpunit/tests/query/metaQuery.php tests/phpunit/tests/query/metaQuery.php
index 8f9b212..2eff8eb 100644
--- tests/phpunit/tests/query/metaQuery.php
+++ tests/phpunit/tests/query/metaQuery.php
@@ -1558,4 +1558,101 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
 		$posts = wp_list_pluck( $posts, 'ID' );
 		$this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
 	}
+
+	/**
+	 * @ticket 31045
+	 */
+	public function test_orderby_name() {
+		$posts = $this->factory->post->create_many( 3 );
+		add_post_meta( $posts[0], 'foo', 'aaa' );
+		add_post_meta( $posts[1], 'foo', 'zzz' );
+		add_post_meta( $posts[2], 'foo', 'jjj' );
+
+		$q = new WP_Query( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'name' => 'foo_name',
+					'key' => 'foo',
+					'compare' => 'EXISTS',
+				),
+			),
+			'orderby' => 'foo_name',
+			'order' => 'DESC',
+		) );
+
+		$this->assertEquals( array( $posts[1], $posts[2], $posts[0] ), $q->posts );
+	}
+
+	/**
+	 * @ticket 31045
+	 */
+	public function test_orderby_name_as_secondary_sort() {
+		$p1 = $this->factory->post->create( array(
+			'post_date' => '2015-01-28 03:00:00',
+		) );
+		$p2 = $this->factory->post->create( array(
+			'post_date' => '2015-01-28 05:00:00',
+		) );
+		$p3 = $this->factory->post->create( array(
+			'post_date' => '2015-01-28 03:00:00',
+		) );
+
+		add_post_meta( $p1, 'foo', 'jjj' );
+		add_post_meta( $p2, 'foo', 'zzz' );
+		add_post_meta( $p3, 'foo', 'aaa' );
+
+		$q = new WP_Query( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'name' => 'foo_name',
+					'key' => 'foo',
+					'compare' => 'EXISTS',
+				),
+			),
+			'orderby' => array(
+				'post_date' => 'asc',
+				'foo_name' => 'asc',
+			),
+		) );
+
+		$this->assertEquals( array( $p3, $p1, $p2 ), $q->posts );
+	}
+
+	/**
+	 * @ticket 31045
+	 */
+	public function test_orderby_more_than_one_name() {
+		$posts = $this->factory->post->create_many( 3 );
+
+		add_post_meta( $posts[0], 'foo', 'jjj' );
+		add_post_meta( $posts[1], 'foo', 'zzz' );
+		add_post_meta( $posts[2], 'foo', 'jjj' );
+		add_post_meta( $posts[0], 'bar', 'aaa' );
+		add_post_meta( $posts[1], 'bar', 'ccc' );
+		add_post_meta( $posts[2], 'bar', 'bbb' );
+
+		$q = new WP_Query( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'name' => 'foo_name',
+					'key' => 'foo',
+					'compare' => 'EXISTS',
+				),
+				array(
+					'name' => 'bar_name',
+					'key' => 'bar',
+					'compare' => 'EXISTS',
+				),
+			),
+			'orderby' => array(
+				'foo_name' => 'asc',
+				'bar_name' => 'desc',
+			),
+		) );
+
+		$this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $q->posts );
+	}
 }
