Make WordPress Core


Ignore:
Timestamp:
10/13/2014 10:02:18 PM (12 years ago)
Author:
boonebgorges
Message:

Introduce support for nested queries in WP_Meta_Query.

Previously, meta query arguments could be joined by a single AND or OR relation.
Now, these queries can be arbitrarily nested, allowing clauses to be linked
together with multiple relations.

Adds unit tests for the new nesting syntax. Modifies a few existing unit tests
that were overly specific for the old SQL syntax. Backward compatibility with
existing syntax is fully maintained.

Props boonebgorges, DrewAPicture.
See #29642.

File:
1 edited

Legend:

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

    r29857 r29887  
    773773        }
    774774
     775        /**
     776         * @ticket 29642
     777         * @group meta
     778         */
     779        public function test_meta_query_nested() {
     780                $p1 = $this->factory->post->create();
     781                $p2 = $this->factory->post->create();
     782                $p3 = $this->factory->post->create();
     783
     784                add_post_meta( $p1, 'foo', 'bar' );
     785                add_post_meta( $p2, 'foo2', 'bar' );
     786                add_post_meta( $p3, 'foo2', 'bar' );
     787                add_post_meta( $p3, 'foo3', 'bar' );
     788
     789                $query = new WP_Query( array(
     790                        'update_post_meta_cache' => false,
     791                        'update_term_meta_cache' => false,
     792                        'fields' => 'ids',
     793                        'meta_query' => array(
     794                                'relation' => 'OR',
     795                                array(
     796                                        'key' => 'foo',
     797                                        'value' => 'bar',
     798                                ),
     799                                array(
     800                                        'relation' => 'AND',
     801                                        array(
     802                                                'key' => 'foo2',
     803                                                'value' => 'bar',
     804                                        ),
     805                                        array(
     806                                                'key' => 'foo3',
     807                                                'value' => 'bar',
     808                                        ),
     809                                ),
     810                        ),
     811                ) );
     812
     813                $expected = array( $p1, $p3 );
     814                $this->assertEqualSets( $expected, $query->posts );
     815        }
     816
     817        /**
     818         * @ticket 29642
     819         * @group meta
     820         */
     821        public function test_meta_query_nested_two_levels_deep() {
     822                $p1 = $this->factory->post->create();
     823                $p2 = $this->factory->post->create();
     824                $p3 = $this->factory->post->create();
     825
     826                add_post_meta( $p1, 'foo', 'bar' );
     827                add_post_meta( $p3, 'foo2', 'bar' );
     828                add_post_meta( $p3, 'foo3', 'bar' );
     829                add_post_meta( $p3, 'foo4', 'bar' );
     830
     831                $query = new WP_Query( array(
     832                        'update_post_meta_cache' => false,
     833                        'update_term_meta_cache' => false,
     834                        'fields' => 'ids',
     835                        'meta_query' => array(
     836                                'relation' => 'OR',
     837                                array(
     838                                        'key' => 'foo',
     839                                        'value' => 'bar',
     840                                ),
     841                                array(
     842                                        'relation' => 'OR',
     843                                        array(
     844                                                'key' => 'foo2',
     845                                                'value' => 'bar',
     846                                        ),
     847                                        array(
     848                                                'relation' => 'AND',
     849                                                array(
     850                                                        'key' => 'foo3',
     851                                                        'value' => 'bar',
     852                                                ),
     853                                                array(
     854                                                        'key' => 'foo4',
     855                                                        'value' => 'bar',
     856                                                ),
     857                                        ),
     858                                ),
     859                        ),
     860                ) );
     861
     862                $expected = array( $p1, $p3 );
     863                $this->assertEqualSets( $expected, $query->posts );
     864        }
     865
     866        /**
     867         * @group meta
     868         */
    775869        function test_meta_between_not_between() {
    776870                $post_id = $this->factory->post->create();
    … …  
    820914        /**
    821915         * @ticket 16829
     916         * @group meta
    822917         */
    823918        function test_meta_default_compare() {
    … …  
    860955        /**
    861956         * @ticket 17264
     957         * @group meta
    862958         */
    863959        function test_duplicate_posts_when_no_key() {
    … …  
    891987        /**
    892988         * @ticket 15292
     989         * @group meta
    893990         */
    894991        function test_empty_meta_value() {
Note: See TracChangeset for help on using the changeset viewer.