[wp-trac] [WordPress Trac] #59351: Add support for querying by an exact date with WP_Date_Query

WordPress Trac noreply at wordpress.org
Thu Sep 14 20:45:11 UTC 2023


#59351: Add support for querying by an exact date with WP_Date_Query
-------------------------+-----------------------------
 Reporter:  sean212      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Query        |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 WP_Date_Query lacks the ability to query for posts with a specific date.
 Currently, to query for a post with a specific date string you would need
 to break it down to multiple arguments:

 {{{#!php
 <?php
 $date = strtotime( '2023-08-13 20:12:50' );

 $posts = new WP_Query( [
   'date_query' => [
     'year' => date( 'Y', $date ),
     'month' => date( 'm', $date ),
     'day' => date( 'd', $date ),
     'hour' => date( 'H', $date ),
     'minute' => date( 'i', $date ),
     'second' => date( 's', $date ),
   ],
 ] );
 }}}

 There is no argument on WP_Date_Query that accepts a
 `strtotime()`-compatible string for an exact date match. One workaround is
 to use a combination of before/after to get a simplified query.

 Adding support for an exact match can be used when querying for posts from
 a specific date as well as including/excluding posts from a specific date
 for a complex query. Suggested syntax:

 {{{
 // Query for posts after January 1st, 2013 OR exactly on November 7th,
 2010
 [
   'date_query' => [
     'relation' => 'OR',
     [
       'after' => 'January 1st, 2013',
     ],
     [
       'exact' => 'November 7th, 2010',
     ],
   ],
 ]
 }}}

 Relates to #18694.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59351>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list