Last active
December 16, 2015 02:49
-
-
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
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
<?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