Skip to content

Instantly share code, notes, and snippets.

@marteinn
Created July 7, 2011 08:53
Show Gist options
  • Save marteinn/1069123 to your computer and use it in GitHub Desktop.
Save marteinn/1069123 to your computer and use it in GitHub Desktop.
Wordpress - how to filter posts by date (using query parameters as hook arguments)
<?php
// Create hook funktion that alters the sql. It takes two args. $where (the sql string) and $wp_query (which is the actual query and holds the args for the function, $beinDate and $endDate).
function posts_where_hook( $where = '', $wp_query )
{
$beginDate = $wp_query->get("beginDate");
$endDate = $wp_query->get("endDate");
$where .= " AND post_date >= '".$beginDate."' AND post_date < '".$endDate."'";
return $where;
}
// Add hook
add_filter( 'posts_where', 'posts_where_hook', 10, 2 );
// Run the query. The two last arguments will be accessed by the hook using $wp_query->get
query_posts( "cat=-5&beginDate=".$beginDate."&endDate=".$endDate);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment