Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of a relative date query from the WordPress codex.
<?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