Make WordPress Core


Ignore:
Timestamp:
10/17/2014 01:19:03 AM (12 years ago)
Author:
boonebgorges
Message:

Use table aliases for columns in SQL generated by WP_Date_Query.

The use of non-aliased column names (eg 'post_date' instead of 'wp_posts.post_date')
in WP_Date_Query causes SQL notices and other failures when queries involve
table joins, such as date_query combined with tax_query or meta_query. This
changeset modifies WP_Date_Query::validate_column() to add the table alias when
it can be detected from the column name (ie, in the case of core columns).

A side effect of this change is that it is now possible to use WP_Date_Query
to build WHERE clauses across multiple tables, though there is currently no
core support for the automatic generation of the necessary JOIN clauses. See

Props ew_holmes, wonderboymusic, neoxx, Viper007Bond, boonebgorges.
Fixes #25775.

File:
1 edited

Legend:

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

    r29923 r29933  
    611611
    612612        public function test_date_params_monthnum_m_duplicate() {
     613                global $wpdb;
     614
    613615                $this->create_posts();
    614616
     
    630632                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    631633
    632                 $this->assertContains( "MONTH( post_date ) = 5", $this->q->request );
    633                 $this->assertNotContains( "MONTH( post_date ) = 9", $this->q->request );
     634                $this->assertContains( "MONTH( $wpdb->posts.post_date ) = 5", $this->q->request );
     635                $this->assertNotContains( "MONTH( $wpdb->posts.post_date ) = 9", $this->q->request );
    634636        }
    635637
    636638        public function test_date_params_week_w_duplicate() {
     639                global $wpdb;
     640
    637641                $this->create_posts();
    638642
     
    653657                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    654658
    655                 $this->assertContains( "WEEK( post_date, 1 ) = 21", $this->q->request );
    656                 $this->assertNotContains( "WEEK( post_date, 1 ) = 22", $this->q->request );
     659                $this->assertContains( "WEEK( $wpdb->posts.post_date, 1 ) = 21", $this->q->request );
     660                $this->assertNotContains( "WEEK( $wpdb->posts.post_date, 1 ) = 22", $this->q->request );
     661        }
     662
     663        /**
     664         * @ticket 25775
     665         */
     666        public function test_date_query_with_taxonomy_join() {
     667                $p1 = $this->factory->post->create( array(
     668                        'post_date' => '2013-04-27 01:01:01',
     669                ) );
     670                $p2 = $this->factory->post->create( array(
     671                        'post_date' => '2013-03-21 01:01:01',
     672                ) );
     673
     674                register_taxonomy( 'foo', 'post' );
     675                wp_set_object_terms( $p1, 'bar', 'foo' );
     676
     677                $posts = $this->_get_query_result( array(
     678                        'date_query' => array(
     679                                'year' => 2013,
     680                        ),
     681                        'tax_query' => array(
     682                                array(
     683                                        'taxonomy' => 'foo',
     684                                        'terms' => array( 'bar' ),
     685                                        'field' => 'name',
     686                                ),
     687                        ),
     688                ) );
     689
     690                _unregister_taxonomy( 'foo' );
     691
     692                $this->assertEquals( array( $p1 ), wp_list_pluck( $posts, 'ID' ) );
    657693        }
    658694
Note: See TracChangeset for help on using the changeset viewer.