Make WordPress Core


Ignore:
Timestamp:
09/19/2019 03:02:20 PM (7 years ago)
Author:
boonebgorges
Message:

Query: Expand the list of operators available to compare_key in WP_Meta_Query.

compare_key, introduced in #42409, previously supported only = and LIKE
operators. This changeset introduces a number of other operators: !=, IN,
NOT IN, NOT LIKE, RLIKE, REGEXP, NOT REGEXP, EXISTS, and NOT EXISTS
(the latter two aliased to = and !=, respectively). To support case-sensitive
regular expression key comparisons, the new type_key parameter will force
a MySQL CAST when 'BINARY' is passed.

Props soulseekah.
Fixes #43346.

File:
1 edited

Legend:

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

    r42343 r46188  
    739739
    740740        /**
     741         * Verifies only that meta_type_key is passed. See query/metaQuery.php for more complete tests.
     742         *
     743         * @ticket 43446
     744         */
     745        public function test_meta_type_key_should_be_passed_to_meta_query() {
     746                $posts = self::factory()->post->create_many( 3 );
     747
     748                add_post_meta( $posts[0], 'AAA_FOO_AAA', 'abc' );
     749                add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' );
     750                add_post_meta( $posts[2], 'aaa_foo_bbb', 'abc' );
     751                add_post_meta( $posts[2], 'aaa_foo_aaa', 'abc' );
     752
     753                $q = new WP_Query(
     754                        array(
     755                                'meta_key'         => 'AAA_foo_.*',
     756                                'meta_compare_key' => 'REGEXP',
     757                                'fields'           => 'ids',
     758                        )
     759                );
     760
     761                $this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts );
     762
     763                $q = new WP_Query(
     764                        array(
     765                                'meta_key'         => 'AAA_FOO_.*',
     766                                'meta_compare_key' => 'REGEXP',
     767                                'meta_type_key'    => 'BINARY',
     768                                'fields'           => 'ids',
     769                                'fields'           => 'ids',
     770                        )
     771                );
     772
     773                $this->assertEqualSets( array( $posts[0] ), $q->posts );
     774        }
     775
     776        /**
    741777         * This is the clause that ensures that empty arrays are not valid queries.
    742778         */
Note: See TracChangeset for help on using the changeset viewer.