0
votes

This question only applies to the Genesis Framework.

I have a category called "News." On my "News" category page I would like to only show posts that have been made in the last 30 days.

Here is the code I started out with:

<?php

remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'pm_recent_news_loop');

function pm_last_name_loop() {
global $query_args;
$args = (array(
   //????
));

genesis_custom_loop( wp_parse_args($query_args, $args) );

}
genesis();

In the general WordPress API documentation it basically says that you can't achieve filtering by date range by passing in arguments to the $query_args (which is the way that genesis_custom_loop accomplishes it. Instead, it seems that the correct way to do this is to add_filter ('posts_where', 'my_where_filter') and then define the where clause by post_date. Ref: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters.

However I can't get this to work. When I attempt to implement code similar to that described in the codex I either get none of my posts or I get all of my posts. I assume that this is because Genesis somehow works around the posts_where filter or wraps it in such a way that it doesn't get called.

1

1 Answers

1
votes

Try adding a filter instead, with some SQL

filter_daterange($where='') {
$where .= " AND post_date >= '".date('Y-m-d H:i:s', strtotime('-30 days'))."' AND post_date <= '".date('Y-m-d H:i:s')."'";
return $where;
}

Add the filter before your loop, and remove it afterwards

add_filter( 'posts_where', 'filter_daterange' );
genesis_custom_loop( wp_parse_args($query_args, $args) );
remove_filter( 'posts_where', 'filter_daterange' );