Make WordPress Core


Ignore:
Timestamp:
06/04/2014 05:47:19 AM (10 years ago)
Author:
wonderboymusic
Message:

Fix SQL generation when meta_query has an 'relation' => 'OR' for its queries and wants to 'orderby' => 'meta_value'.

Adds unit test.

Props jackreichert.
Fixes #25538.

File:
1 edited

Legend:

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

    r27562 r28659  
    198198        }
    199199    }
     200
     201    function test_query_meta_query_order() {
     202        $post1 = $this->factory->post->create( array( 'post_title' => 'meta-value-1', 'post_date' => '2007-01-01 00:00:00' ) );
     203        $post2 = $this->factory->post->create( array( 'post_title' => 'meta-value-2', 'post_date' => '2007-01-01 00:00:00' ) );
     204        $post3 = $this->factory->post->create( array( 'post_title' => 'meta-value-3', 'post_date' => '2007-01-01 00:00:00' ) );
     205
     206        add_post_meta( $post1, 'order', 1 );
     207        add_post_meta( $post2, 'order', 2 );
     208        add_post_meta( $post3, 'order', 3 );
     209
     210        $args = array(
     211            'post_type' => 'post',
     212            'meta_key' => 'order',
     213            'meta_value' => 1,
     214            'meta_compare' => '>=',
     215            'orderby' => 'meta_value'
     216        );
     217
     218        $args2 = array(
     219            'post_type' => 'post',
     220            'meta_key' => 'order',
     221            'meta_value' => 1,
     222            'meta_compare' => '>=',
     223            'orderby' => 'meta_value',
     224            'meta_query' => array(
     225                'relation' => 'OR',
     226                array(
     227                    'key' => 'order',
     228                    'compare' => '>=',
     229                    'value' => 1
     230                )
     231            )
     232        );
     233
     234        $posts = get_posts( $args );
     235        $posts2 = get_posts( $args2 );
     236
     237        $this->assertEquals( wp_list_pluck( $posts, 'post_title' ), wp_list_pluck( $posts2, 'post_title' ) );
     238    }
    200239}
Note: See TracChangeset for help on using the changeset viewer.