Skip to content

Instantly share code, notes, and snippets.

@graemegeorge
Created November 5, 2012 11:27
Show Gist options
  • Save graemegeorge/4016764 to your computer and use it in GitHub Desktop.
Save graemegeorge/4016764 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'])) {
// Create a new filtering function that will add our where clause to the query
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);
remove_filter( 'posts_where', 'filter_where' );
}
print_r($args);
$keyword = $_GET['s'];
$cat = $_GET['cat'];
$country = $_GET['country'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment