Skip to content

Instantly share code, notes, and snippets.

@graemegeorge
Created November 5, 2012 11:30
Show Gist options
  • Save graemegeorge/4016769 to your computer and use it in GitHub Desktop.
Save graemegeorge/4016769 to your computer and use it in GitHub Desktop.
WP_query and date range filter
// 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' );
}
@ivankravchenko
Copy link

good example
but beware of using it on production, it is sql-injectable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment