[wp-trac] [WordPress Trac] #18694: Improved date arguments for WP_Query

WordPress Trac wp-trac at lists.automattic.com
Mon Sep 26 08:36:35 UTC 2011


#18694: Improved date arguments for WP_Query
--------------------------+------------------------------
 Reporter:  Viper007Bond  |       Owner:  viper007bond
     Type:  enhancement   |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:  3.3
 Severity:  normal        |  Resolution:
 Keywords:                |
--------------------------+------------------------------

Comment (by Viper007Bond):

 Okay, attached is a patch that contains what should be a fully flushed out
 `WP_Date_Query` class. The current integration into `WP_Query` is just
 preliminary and for testing purposes. It will need to be replaced with
 some code that uses `WP_Date_Query` to process the legacy date/time
 parameters. There's also a bit more PHPdoc work to be done.

 For now though this patch will do nicely for testing and feedback which is
 what I'm really after at this point.

 Here's some examples that you can use to test:

 {{{
 // Between 8:45 AM and 5:12 PM on weekdays
 $query_test = new WP_Query( array(
         'date_query' => array(
                 'column' => 'post_date', // Default
                 'relation' => 'AND', // Default
                 array(
                         'hour' => 8,
                         'minute' => 45,
                         'compare' => '>=',
                 ),
                 array(
                         'hour' => 17,
                         'minute' => 12,
                         'compare' => '<=',
                 ),
                 array(
                         'dayofweek' => array( 2, 6 ),
                         'compare' => 'BETWEEN',
                 ),
         ),
 ) );
 }}}

 {{{
 // Posted between 2-3 weeks ago
 $query_test = new WP_Query( array(
         'date_query' => array(
                 array(
                         'after' => '3 weeks ago',
                         'before' => '2 weeks ago',
                 ),
         ),
 ) );
 }}}

 {{{
 // Posted before March 1st, 2010
 $query_test = new WP_Query( array(
         'date_query' => array(
                 array(
                         'before' => array(
                                 'year' => 2010,
                                 'month' => 3,
                                 'day' => 1,
                         ),
                 ),
         ),
 ) );
 }}}

 {{{
 // Posted during the 2 PM hour on a Sunday or during the 6 PM hour on a
 Monday or Tuesday
 $query_test = new WP_Query( array(
         'date_query' => array(
                 'relation' => 'OR',
                 array(
                         'hour' => 14,
                         'dayofweek' => 1,
                 ),
                 array(
                         'hour' => 18, // Will be cast to an array
                         'dayofweek' => array( 2, 3 ),
                         'compare' => 'IN',
                 ),
         ),
 ) );
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18694#comment:14>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list