Skip to content

Instantly share code, notes, and snippets.

@eddiemoya
Last active August 5, 2022 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddiemoya/5365302 to your computer and use it in GitHub Desktop.
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()`
<?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