Make WordPress Core

Changeset 28935


Ignore:
Timestamp:
07/01/2014 01:17:39 AM (10 years ago)
Author:
SergeyBiryukov
Message:

WP_Date_Query: The inclusive logic should include all times within the date range.

props mboynes, oso96_2000, DrewAPicture.
fixes #26653.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/date.php

    r28532 r28935  
    4848    /**
    4949     * Constructor.
     50     *
     51     * @since 3.7.0
     52     * @since 4.0.0 The $inclusive logic was updated to include all times within the date range.
    5053     *
    5154     * @param array $date_query {
     
    236239        $compare = $this->get_compare( $query );
    237240
     241        $inclusive = ! empty( $query['inclusive'] );
     242
     243        // Assign greater- and less-than values.
    238244        $lt = '<';
    239245        $gt = '>';
    240         if ( ! empty( $query['inclusive'] ) ) {
     246
     247        if ( $inclusive ) {
    241248            $lt .= '=';
    242249            $gt .= '=';
     
    245252        // Range queries
    246253        if ( ! empty( $query['after'] ) )
    247             $where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], true ) );
     254            $where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
    248255
    249256        if ( ! empty( $query['before'] ) )
    250             $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], false ) );
     257            $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
    251258
    252259        // Specific value queries
  • trunk/tests/phpunit/tests/query/dateQuery.php

    r28252 r28935  
    255255    }
    256256
     257    /**
     258     * @ticket 26653
     259     */
     260    public function test_date_query_inclusive_between_dates() {
     261        $posts = $this->_get_query_result( array(
     262            'date_query' => array(
     263                'after' => array(
     264                    'year' => 2007,
     265                    'month' => 1
     266                ),
     267                'before' => array(
     268                    'year' => 2008,
     269                    'month' => 12
     270                ),
     271                'inclusive' => true
     272            ),
     273        ) );
     274
     275
     276        $expected_dates = array(
     277            '2007-01-22 03:49:21',
     278            '2007-05-16 17:32:22',
     279            '2007-09-24 07:17:23',
     280            '2008-03-29 09:04:25',
     281            '2008-07-15 11:32:26',
     282            '2008-12-10 13:06:27',
     283        );
     284
     285        $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     286    }
     287
    257288    public function test_date_query_year_expecting_results() {
    258289        $posts = $this->_get_query_result( array(
Note: See TracChangeset for help on using the changeset viewer.