I want to change the order of a feed using pre_get_posts filter. But I want to exclude dates older than today. I see how one could do that if the date were in a meta key, but the date is in post_date. How can I check post_date to only show posts later than today? The site shows future post dates.

is_feed) { $query->set('order','ASC'); } return $query; } add_filter('pre_get_posts','feedFilter'); ?>

You can't really check post_date in pre_get_posts, because, well, the action fires before (pre_...) the posts have actually been fetched (...get_posts). :)

But, you can use $query->set() to add date parameters to the query. This is taken from an example in the WP_Query() Codex entry:

function wpse54142_filter_pre_get_posts( $query ) {
    if ( is_feed() ) {
        $today = getdate();
        $query->set( 'year', $today['year'] );
        $query->set( 'monthnum', $today['mon'] );
        $query->set( 'day', $today['mday'] );
    return $query;
add_filter( 'pre_get_posts', 'wpse54142_filter_pre_get_posts' );
| improve this answer | |
  • correct me if I'm wrong, but this only shows posts on that day, not into the future from that day. – Milo Jun 4 '12 at 14:04
  • From the OP: "But I want to exclude dates older than today.". Why would an RSS feed ever need to show future posts? – Chip Bennett Jun 4 '12 at 14:08
  • I don't know, but also from the OP: "The site shows future post dates" – Milo Jun 4 '12 at 14:09

Here's an example using a posts_where filter:

function wpa54142_feed_filter( $query ) {
    if ( $query->is_feed ) {
        $query->set( 'order', 'ASC' );
        add_filter( 'posts_where', 'wpa54142_filter_where' );
    return $query;
add_filter( 'pre_get_posts', 'wpa54142_feed_filter' );

function wpa54142_filter_where( $where = '' ) {
    $today = date( 'Y-m-d' );
    $where .= " AND post_date >= '$today'";
    return $where;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.