Created
March 23, 2022 10:16
-
-
Save carlaizumibamford/055cdab43de9a47956cd8e6f09fe8b9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Returns posts dated December 12, 2012: | |
$query = new WP_Query( 'year=2012&monthnum=12&day=12' ); | |
or: | |
$args = array( | |
'date_query' => array( | |
array( | |
'year' => 2012, | |
'month' => 12, | |
'day' => 12, | |
), | |
), | |
); | |
$query = new WP_Query( $args ); | |
Returns posts for today: | |
$today = getdate(); | |
$query = new WP_Query( 'year=' . $today['year'] . '&monthnum=' . $today['mon'] . '&day=' . $today['mday'] ); | |
or: | |
$today = getdate(); | |
$args = array( | |
'date_query' => array( | |
array( | |
'year' => $today['year'], | |
'month' => $today['mon'], | |
'day' => $today['mday'], | |
), | |
), | |
); | |
$query = new WP_Query( $args ); | |
Returns posts for this week: | |
$week = date( 'W' ); | |
$year = date( 'Y' ); | |
$query = new WP_Query( 'year=' . $year . '&w=' . $week ); | |
or: | |
$args = array( | |
'date_query' => array( | |
array( | |
'year' => date( 'Y' ), | |
'week' => date( 'W' ), | |
), | |
), | |
); | |
$query = new WP_Query( $args ); | |
Return posts between 9AM to 5PM on weekdays | |
$args = array( | |
'date_query' => array( | |
array( | |
'hour' => 9, | |
'compare' => '>=', | |
), | |
array( | |
'hour' => 17, | |
'compare' => '<=', | |
), | |
array( | |
'dayofweek' => array( 2, 6 ), | |
'compare' => 'BETWEEN', | |
), | |
), | |
'posts_per_page' => -1, | |
); | |
$query = new WP_Query( $args ); | |
Return posts from January 1st to February 28th | |
$args = array( | |
'date_query' => array( | |
array( | |
'after' => 'January 1st, 2013', | |
'before' => array( | |
'year' => 2013, | |
'month' => 2, | |
'day' => 28, | |
), | |
'inclusive' => true, | |
), | |
), | |
'posts_per_page' => -1, | |
); | |
$query = new WP_Query( $args ); | |
Note that if a strtotime()-compatible string with just a date was passed in the before parameter, this will be converted to 00:00:00 on that date. In this case, even if inclusive was set to true, the date would not be included in the query. If you want a before date to be inclusive, include the time as well, such as 'before' => '2013-02-28 23:59:59', or use the array format, which is adjusted automatically if inclusive is set. | |
Return posts made over a year ago but modified in the past month | |
$args = array( | |
'date_query' => array( | |
array( | |
'column' => 'post_date_gmt', | |
'before' => '1 year ago', | |
), | |
array( | |
'column' => 'post_modified_gmt', | |
'after' => '1 month ago', | |
), | |
), | |
'posts_per_page' => -1, | |
); | |
$query = new WP_Query( $args ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment