Make WordPress Core


Ignore:
Timestamp:
01/31/2015 03:47:51 PM (10 years ago)
Author:
boonebgorges
Message:

Improve support for ordering WP_Query results by postmeta.

WP_Meta_Query clauses now support a 'name' parameter. When building a
WP_Query object, the value of 'orderby' can reference this 'name', so that
it's possible to order by any clause in a meta_query, not just the first one
(as when using 'orderby=meta_value'). This improvement also makes it possible
to order by multiple meta query clauses (or by any other eligible field plus
a meta query clause), using the array syntax for 'orderby' introduced in [29027].

Props Funkatronic, boonebgorges.
Fixes #31045.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/metaQuery.php

    r31286 r31312  
    15591559        $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
    15601560    }
     1561
     1562    /**
     1563     * @ticket 31045
     1564     */
     1565    public function test_orderby_name() {
     1566        $posts = $this->factory->post->create_many( 3 );
     1567        add_post_meta( $posts[0], 'foo', 'aaa' );
     1568        add_post_meta( $posts[1], 'foo', 'zzz' );
     1569        add_post_meta( $posts[2], 'foo', 'jjj' );
     1570
     1571        $q = new WP_Query( array(
     1572            'fields' => 'ids',
     1573            'meta_query' => array(
     1574                array(
     1575                    'name' => 'foo_name',
     1576                    'key' => 'foo',
     1577                    'compare' => 'EXISTS',
     1578                ),
     1579            ),
     1580            'orderby' => 'foo_name',
     1581            'order' => 'DESC',
     1582        ) );
     1583
     1584        $this->assertEquals( array( $posts[1], $posts[2], $posts[0] ), $q->posts );
     1585    }
     1586
     1587    /**
     1588     * @ticket 31045
     1589     */
     1590    public function test_orderby_name_as_secondary_sort() {
     1591        $p1 = $this->factory->post->create( array(
     1592            'post_date' => '2015-01-28 03:00:00',
     1593        ) );
     1594        $p2 = $this->factory->post->create( array(
     1595            'post_date' => '2015-01-28 05:00:00',
     1596        ) );
     1597        $p3 = $this->factory->post->create( array(
     1598            'post_date' => '2015-01-28 03:00:00',
     1599        ) );
     1600
     1601        add_post_meta( $p1, 'foo', 'jjj' );
     1602        add_post_meta( $p2, 'foo', 'zzz' );
     1603        add_post_meta( $p3, 'foo', 'aaa' );
     1604
     1605        $q = new WP_Query( array(
     1606            'fields' => 'ids',
     1607            'meta_query' => array(
     1608                array(
     1609                    'name' => 'foo_name',
     1610                    'key' => 'foo',
     1611                    'compare' => 'EXISTS',
     1612                ),
     1613            ),
     1614            'orderby' => array(
     1615                'post_date' => 'asc',
     1616                'foo_name' => 'asc',
     1617            ),
     1618        ) );
     1619
     1620        $this->assertEquals( array( $p3, $p1, $p2 ), $q->posts );
     1621    }
     1622
     1623    /**
     1624     * @ticket 31045
     1625     */
     1626    public function test_orderby_more_than_one_name() {
     1627        $posts = $this->factory->post->create_many( 3 );
     1628
     1629        add_post_meta( $posts[0], 'foo', 'jjj' );
     1630        add_post_meta( $posts[1], 'foo', 'zzz' );
     1631        add_post_meta( $posts[2], 'foo', 'jjj' );
     1632        add_post_meta( $posts[0], 'bar', 'aaa' );
     1633        add_post_meta( $posts[1], 'bar', 'ccc' );
     1634        add_post_meta( $posts[2], 'bar', 'bbb' );
     1635
     1636        $q = new WP_Query( array(
     1637            'fields' => 'ids',
     1638            'meta_query' => array(
     1639                array(
     1640                    'name' => 'foo_name',
     1641                    'key' => 'foo',
     1642                    'compare' => 'EXISTS',
     1643                ),
     1644                array(
     1645                    'name' => 'bar_name',
     1646                    'key' => 'bar',
     1647                    'compare' => 'EXISTS',
     1648                ),
     1649            ),
     1650            'orderby' => array(
     1651                'foo_name' => 'asc',
     1652                'bar_name' => 'desc',
     1653            ),
     1654        ) );
     1655
     1656        $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $q->posts );
     1657    }
    15611658}
Note: See TracChangeset for help on using the changeset viewer.