Created
July 7, 2011 08:53
-
-
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)
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 | |
// 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