diff --git src/wp-includes/meta.php src/wp-includes/meta.php
index c0430f7..36d6d1d 100644
|
|
class WP_Meta_Query { |
945 | 945 | * Constructor. |
946 | 946 | * |
947 | 947 | * @since 3.2.0 |
948 | | * @since 4.2.0 Introduced the `$name` parameter, for improved `$orderby` support in the parent query. |
| 948 | * @since 4.2.0 Introduced support for naming query clauses by associative array keys. |
949 | 949 | * |
950 | 950 | * @access public |
951 | 951 | * |
952 | 952 | * @param array $meta_query { |
953 | | * Array of meta query clauses. |
| 953 | * Array of meta query clauses. First-order clauses may use strings for array keys, in order to reference |
| 954 | * the clause in the 'orderby' parameter of the parent query. |
954 | 955 | * |
955 | 956 | * @type string $relation Optional. The MySQL keyword used to join |
956 | 957 | * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'. |
… |
… |
class WP_Meta_Query { |
967 | 968 | * comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE', |
968 | 969 | * 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'. |
969 | 970 | * Default is 'CHAR'. |
970 | | * @type string $name Optional. A unique identifier for the clause. If provided, `$name` can be |
971 | | * referenced in the `$orderby` parameter of the parent query. |
972 | 971 | * } |
973 | 972 | * } |
974 | 973 | */ |
… |
… |
class WP_Meta_Query { |
1016 | 1015 | unset( $query['value'] ); |
1017 | 1016 | } |
1018 | 1017 | |
1019 | | $clean_queries[] = $query; |
| 1018 | $clean_queries[ $key ] = $query; |
1020 | 1019 | |
1021 | 1020 | // Otherwise, it's a nested query, so we recurse. |
1022 | 1021 | } else { |
1023 | 1022 | $cleaned_query = $this->sanitize_query( $query ); |
1024 | 1023 | |
1025 | 1024 | if ( ! empty( $cleaned_query ) ) { |
1026 | | $clean_queries[] = $cleaned_query; |
| 1025 | $clean_queries[ $key ] = $cleaned_query; |
1027 | 1026 | } |
1028 | 1027 | } |
1029 | 1028 | } |
… |
… |
class WP_Meta_Query { |
1270 | 1269 | |
1271 | 1270 | // This is a first-order clause. |
1272 | 1271 | if ( $this->is_first_order_clause( $clause ) ) { |
1273 | | $clause_sql = $this->get_sql_for_clause( $clause, $query ); |
| 1272 | $clause_sql = $this->get_sql_for_clause( $clause, $query, $key ); |
1274 | 1273 | |
1275 | 1274 | $where_count = count( $clause_sql['where'] ); |
1276 | 1275 | if ( ! $where_count ) { |
… |
… |
class WP_Meta_Query { |
1330 | 1329 | * @type string $where SQL fragment to append to the main WHERE clause. |
1331 | 1330 | * } |
1332 | 1331 | */ |
1333 | | public function get_sql_for_clause( &$clause, $parent_query ) { |
| 1332 | public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) { |
1334 | 1333 | global $wpdb; |
1335 | 1334 | |
1336 | 1335 | $sql_chunks = array( |
… |
… |
class WP_Meta_Query { |
1392 | 1391 | $clause['cast'] = $meta_type; |
1393 | 1392 | |
1394 | 1393 | // Store the clause in our flat array. |
1395 | | $clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias']; |
1396 | | $this->clauses[ $clause_name ] =& $clause; |
| 1394 | if ( ! $clause_key ) { |
| 1395 | $clause_key = $clause['alias']; |
| 1396 | } |
| 1397 | |
| 1398 | $this->clauses[ $clause_key ] =& $clause; |
1397 | 1399 | |
1398 | 1400 | // Next, build the WHERE clause. |
1399 | 1401 | |
… |
… |
class WP_Meta_Query { |
1471 | 1473 | } |
1472 | 1474 | |
1473 | 1475 | /** |
1474 | | * Get a flattened list of sanitized meta clauses, indexed by clause 'name'. |
| 1476 | * Get a flattened list of sanitized meta clauses. |
1475 | 1477 | * |
1476 | 1478 | * This array should be used for clause lookup, as when the table alias and CAST type must be determined for |
1477 | 1479 | * a value of 'orderby' corresponding to a meta clause. |
diff --git src/wp-includes/query.php src/wp-includes/query.php
index 3c1b0c2..c65152e 100644
|
|
class WP_Query { |
1497 | 1497 | * @type int $offset The number of posts to offset before retrieval. |
1498 | 1498 | * @type string $order Designates ascending or descending order of posts. Default 'DESC'. |
1499 | 1499 | * Accepts 'ASC', 'DESC'. |
1500 | | * @type string $orderby Sort retrieved posts by parameter. One or more options can be |
| 1500 | * @type string|array $orderby Sort retrieved posts by parameter. One or more options can be |
1501 | 1501 | * passed. To use 'meta_value', or 'meta_value_num', |
1502 | | * 'meta_key=keyname' must be also be defined. Default 'date'. |
1503 | | * Accepts 'none', 'name', 'author', 'date', 'title', 'modified', |
1504 | | * 'menu_order', 'parent', 'ID', 'rand', 'comment_count'. |
| 1502 | * 'meta_key=keyname' must be also be defined. To sort by a |
| 1503 | * specific `$meta_query` clause, use that clause's array key. |
| 1504 | * Default 'date'. Accepts 'none', 'name', 'author', 'date', |
| 1505 | * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', |
| 1506 | * 'comment_count', 'meta_value', 'meta_value_num', and the |
| 1507 | * array keys of `$meta_query`. |
1505 | 1508 | * @type int $p Post ID. |
1506 | 1509 | * @type int $page Show the number of posts that would show up on page X of a |
1507 | 1510 | * static front page. |
diff --git tests/phpunit/tests/query/metaQuery.php tests/phpunit/tests/query/metaQuery.php
index 2eff8eb..d34dc01 100644
|
|
class Tests_Query_MetaQuery extends WP_UnitTestCase { |
1562 | 1562 | /** |
1563 | 1563 | * @ticket 31045 |
1564 | 1564 | */ |
1565 | | public function test_orderby_name() { |
| 1565 | public function test_orderby_clause_key() { |
1566 | 1566 | $posts = $this->factory->post->create_many( 3 ); |
1567 | 1567 | add_post_meta( $posts[0], 'foo', 'aaa' ); |
1568 | 1568 | add_post_meta( $posts[1], 'foo', 'zzz' ); |
… |
… |
class Tests_Query_MetaQuery extends WP_UnitTestCase { |
1571 | 1571 | $q = new WP_Query( array( |
1572 | 1572 | 'fields' => 'ids', |
1573 | 1573 | 'meta_query' => array( |
1574 | | array( |
1575 | | 'name' => 'foo_name', |
| 1574 | 'foo_key' => array( |
1576 | 1575 | 'key' => 'foo', |
1577 | 1576 | 'compare' => 'EXISTS', |
1578 | 1577 | ), |
1579 | 1578 | ), |
1580 | | 'orderby' => 'foo_name', |
| 1579 | 'orderby' => 'foo_key', |
1581 | 1580 | 'order' => 'DESC', |
1582 | 1581 | ) ); |
1583 | 1582 | |
… |
… |
class Tests_Query_MetaQuery extends WP_UnitTestCase { |
1587 | 1586 | /** |
1588 | 1587 | * @ticket 31045 |
1589 | 1588 | */ |
1590 | | public function test_orderby_name_as_secondary_sort() { |
| 1589 | public function test_orderby_clause_key_as_secondary_sort() { |
1591 | 1590 | $p1 = $this->factory->post->create( array( |
1592 | 1591 | 'post_date' => '2015-01-28 03:00:00', |
1593 | 1592 | ) ); |
… |
… |
class Tests_Query_MetaQuery extends WP_UnitTestCase { |
1605 | 1604 | $q = new WP_Query( array( |
1606 | 1605 | 'fields' => 'ids', |
1607 | 1606 | 'meta_query' => array( |
1608 | | array( |
1609 | | 'name' => 'foo_name', |
| 1607 | 'foo_key' => array( |
1610 | 1608 | 'key' => 'foo', |
1611 | 1609 | 'compare' => 'EXISTS', |
1612 | 1610 | ), |
1613 | 1611 | ), |
1614 | 1612 | 'orderby' => array( |
1615 | 1613 | 'post_date' => 'asc', |
1616 | | 'foo_name' => 'asc', |
| 1614 | 'foo_key' => 'asc', |
1617 | 1615 | ), |
1618 | 1616 | ) ); |
1619 | 1617 | |
… |
… |
class Tests_Query_MetaQuery extends WP_UnitTestCase { |
1623 | 1621 | /** |
1624 | 1622 | * @ticket 31045 |
1625 | 1623 | */ |
1626 | | public function test_orderby_more_than_one_name() { |
| 1624 | public function test_orderby_more_than_one_clause_key() { |
1627 | 1625 | $posts = $this->factory->post->create_many( 3 ); |
1628 | 1626 | |
1629 | 1627 | add_post_meta( $posts[0], 'foo', 'jjj' ); |
… |
… |
class Tests_Query_MetaQuery extends WP_UnitTestCase { |
1636 | 1634 | $q = new WP_Query( array( |
1637 | 1635 | 'fields' => 'ids', |
1638 | 1636 | 'meta_query' => array( |
1639 | | array( |
1640 | | 'name' => 'foo_name', |
| 1637 | 'foo_key' => array( |
1641 | 1638 | 'key' => 'foo', |
1642 | 1639 | 'compare' => 'EXISTS', |
1643 | 1640 | ), |
1644 | | array( |
1645 | | 'name' => 'bar_name', |
| 1641 | 'bar_key' => array( |
1646 | 1642 | 'key' => 'bar', |
1647 | 1643 | 'compare' => 'EXISTS', |
1648 | 1644 | ), |
1649 | 1645 | ), |
1650 | 1646 | 'orderby' => array( |
1651 | | 'foo_name' => 'asc', |
1652 | | 'bar_name' => 'desc', |
| 1647 | 'foo_key' => 'asc', |
| 1648 | 'bar_key' => 'desc', |
1653 | 1649 | ), |
1654 | 1650 | ) ); |
1655 | 1651 | |