Skip to content

Instantly share code, notes, and snippets.

@eddiemoya
Last active December 16, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddiemoya/5365361 to your computer and use it in GitHub Desktop.
Save eddiemoya/5365361 to your computer and use it in GitHub Desktop.
Example of a relative date query from the WordPress codex. https://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters
<?php /** NOTE: This is the example from the Codex, which think is BAD. **/
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days (hardcoded)
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
//Requires immediate additiona and removal of the filter for intended use.
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment