Created
November 5, 2012 11:30
-
-
Save graemegeorge/4016769 to your computer and use it in GitHub Desktop.
WP_query and date range filter
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
// Build the wp_query array | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
$args = array( | |
'post_type' => 'post', | |
'show_posts' => '10', | |
'paged' => $paged | |
); | |
foreach(array('s', 'cat', 'country', 'd') as $key) { | |
if(isset($_GET[$key]) && trim($_GET[$key]) !== '') { $args[$key] = $_GET[$key]; } | |
} | |
if(isset($args['s'])) { | |
// This is the date range filter | |
function filter_where( $where = '' ) { | |
$date_range = $_GET['d']; | |
$date_range = explode("|", $date_range); | |
$that_date = $date_range[0]; | |
$this_date = $date_range[1]; | |
$where .= " AND post_date >= '".$that_date."' AND post_date <= '".$this_date."' "; | |
return $where; | |
} | |
add_filter( 'posts_where', 'filter_where' ); | |
$the_query = new WP_Query($args); // WP_QUERY | |
remove_filter( 'posts_where', 'filter_where' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good example
but beware of using it on production, it is sql-injectable