Changeset 31340
- Timestamp:
- 02/05/2015 07:37:47 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/meta.php
r31312 r31340 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. When first-order clauses use strings as their array keys, they may be 954 * referenced in the 'orderby' parameter of the parent query. 954 955 * 955 956 * @type string $relation Optional. The MySQL keyword used to join … … 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 be971 * referenced in the `$orderby` parameter of the parent query.972 971 * } 973 972 * } … … 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. … … 1024 1023 1025 1024 if ( ! empty( $cleaned_query ) ) { 1026 $clean_queries[ ] = $cleaned_query;1025 $clean_queries[ $key ] = $cleaned_query; 1027 1026 } 1028 1027 } … … 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'] ); … … 1322 1321 * @access public 1323 1322 * 1324 * @param array $clause Query clause, passed by reference. 1325 * @param array $parent_query Parent query array. 1323 * @param array $clause Query clause, passed by reference. 1324 * @param array $parent_query Parent query array. 1325 * @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query` 1326 * parameters. If not provided, a key will be generated automatically. 1326 1327 * @return array { 1327 1328 * Array containing JOIN and WHERE SQL clauses to append to a first-order query. … … 1331 1332 * } 1332 1333 */ 1333 public function get_sql_for_clause( &$clause, $parent_query ) {1334 public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) { 1334 1335 global $wpdb; 1335 1336 … … 1392 1393 $clause['cast'] = $meta_type; 1393 1394 1395 // Fallback for clause keys is the table alias. 1396 if ( ! $clause_key ) { 1397 $clause_key = $clause['alias']; 1398 } 1399 1400 // Ensure unique clause keys, so none are overwritten. 1401 $iterator = 1; 1402 $clause_key_base = $clause_key; 1403 while ( isset( $this->clauses[ $clause_key ] ) ) { 1404 $clause_key = $clause_key_base . '-' . $iterator; 1405 $iterator++; 1406 } 1407 1394 1408 // Store the clause in our flat array. 1395 $clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias']; 1396 $this->clauses[ $clause_name ] =& $clause; 1409 $this->clauses[ $clause_key ] =& $clause; 1397 1410 1398 1411 // Next, build the WHERE clause. … … 1472 1485 1473 1486 /** 1474 * Get a flattened list of sanitized meta clauses , indexed by clause 'name'.1487 * Get a flattened list of sanitized meta clauses. 1475 1488 * 1476 1489 * This array should be used for clause lookup, as when the table alias and CAST type must be determined for -
trunk/src/wp-includes/query.php
r31324 r31340 1451 1451 * 1452 1452 * @since 1.5.0 1453 * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's 1454 * array key to `$orderby`. 1453 1455 * @access public 1454 1456 * … … 1498 1500 * @type string $order Designates ascending or descending order of posts. Default 'DESC'. 1499 1501 * Accepts 'ASC', 'DESC'. 1500 * @type string $orderby Sort retrieved posts by parameter. One or more options canbe1502 * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be 1501 1503 * 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'. 1504 * 'meta_key=keyname' must be also be defined. To sort by a 1505 * specific `$meta_query` clause, use that clause's array key. 1506 * Default 'date'. Accepts 'none', 'name', 'author', 'date', 1507 * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 1508 * 'comment_count', 'meta_value', 'meta_value_num', and the 1509 * array keys of `$meta_query`. 1505 1510 * @type int $p Post ID. 1506 1511 * @type int $page Show the number of posts that would show up on page X of a -
trunk/tests/phpunit/tests/query/metaQuery.php
r31312 r31340 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' ); … … 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 ) ); … … 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', … … 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', … … 1614 1612 'orderby' => array( 1615 1613 'post_date' => 'asc', 1616 'foo_ name' => 'asc',1614 'foo_key' => 'asc', 1617 1615 ), 1618 1616 ) ); … … 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 … … 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', … … 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 ) ); … … 1656 1652 $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $q->posts ); 1657 1653 } 1654 1655 /** 1656 * @ticket 31045 1657 */ 1658 public function test_duplicate_clause_keys_should_be_made_unique() { 1659 $q = new WP_Query( array( 1660 'fields' => 'ids', 1661 'meta_query' => array( 1662 'foo_key' => array( 1663 'key' => 'foo', 1664 'compare' => 'EXISTS', 1665 ), 1666 array( 1667 'foo_key' => array( 1668 'key' => 'bar', 1669 'compare' => 'EXISTS', 1670 ), 1671 ), 1672 array( 1673 'foo_key' => array( 1674 'key' => 'baz', 1675 'compare' => 'EXISTS', 1676 ), 1677 ), 1678 ), 1679 ) ); 1680 1681 $this->assertEqualSets( array( 'foo_key', 'foo_key-1', 'foo_key-2' ), array_keys( $q->meta_query->get_clauses() ) ); 1682 } 1658 1683 }
Note: See TracChangeset
for help on using the changeset viewer.