I'm trying to get all of the posts with a custom post type that have been published in the past 5 days.
Although this is overly difficult, and it doesn't look like a better method will be available until 3.7, I've managed to get it working inside of a custom template by filtering adding a filter to posts_where.
The problem is, I need to do this inside of a class and I can't figure out what I'm doing wrong. I'm almost certain that it is my code that is the problem.
Here is the code that works:
function filter_where( $where = '' ) { // posts in the last 5 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-5 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$args = array(
'post_type' => 'listserv',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'listserv',
'field' => 'slug',
'terms' => 'staff-listserv'
)
)
);
$query = new WP_Query( $args );
echo '<div id="content" style="">';
while( $query->have_posts() ) : $query->the_post();
echo '<a href="#">'. get_the_title() .'</a><br/>';
endwhile;
echo '</div>';
remove_filter( 'posts_where', 'filter_where' );
?>
The problem is, when I put everything in to a class and move the second part in to a method, it simply returns the 8 most recently published posts and pages.
Here's what that code looks like:
class Test_Range {
private function filter_where( $where = '' ) { // posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-40 days')) . "'";
return $where;
}
public function range_announcements() {
$filter_where = $this->filter_where();
add_filter( 'posts_where', $filter_where);
$args = array(
'post_type' => 'listserv',
'suppress_filters' => false,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'listserv',
'field' => 'slug',
'terms' => 'staff-listserv'
)
)
);
$query = new WP_Query( $args );
$announcements = '<div id="content" style="">';
while( $query->have_posts() ) : $query->the_post();
$announcements .= '<a href="#">'. get_the_title() .'</a><br/>';
endwhile;
$announcements .= '</div>';
echo $announcements;
remove_filter( 'posts_where', $filter_where );
}
}
$testing_the_range = New Test_Range();
$testing_the_range -> range_announcements();
I've been messing with this for hours and I'm not sure what to try next. Any suggestions?