Last active
August 5, 2022 15:59
-
-
Save eddiemoya/5365302 to your computer and use it in GitHub Desktop.
Allows WP_Query to use a 'since' argument to query for relative date queries using `strtotime()`
This file contains hidden or 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
<?php | |
/** | |
* Allows WP_Query to use a 'since' argument to query for | |
* relative date queries. | |
* | |
* Usage: Query posts from last 30 days | |
* $query = new WP_Query('since' => '-30 days'); | |
* | |
* @uses strtotime() | |
* @author Eddie Moya | |
**/ | |
function filter_where_add_since( $where = '', $query) { | |
if( isset($query->query_vars['since']) ){ | |
$where .= " AND post_date > '" . date('Y-m-d', strtotime($query->query_vars['since'])) . "'"; | |
} | |
return $where; | |
} | |
add_filter( 'posts_where', 'filter_where_add_since', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment