How do I correctly get all posts within the last year using the query_posts function?

Specifically, I am making a custom rss feed. I have modeled it after the rss2 feed that is in the wp-includes folder. I can successfully retrieve all posts using the standard query_posts(‘posts_per_page=-1’).

Here is how I am attempting to get just the ones posted in the past year:

Read More
$today = date('Y-m-d');
$newdate = strtotime ( '-1 year' , strtotime ( $today ) ) ;
$ayearago = date( 'Y-m-d' , $newdate );

function filter_where($where = '') {
        $where .= " AND post_date >= '" . $ayearago . "'";
    return $where;
}

add_filter('posts_where', 'filter_where');

query_posts($query_string);

Yes this returns only posts from 2011 forward. Can someone help me figure out why?

Here is the main source code from my custom feed page called feed-postlist.php:

    /**
 * Custom RSS Feed
 *
 * @package WordPress
 */

header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
$more = 1;

echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>

<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    <?php do_action('rss2_ns'); ?>
>
<?php
$today = date('Y-m-d');
$newdate = strtotime ( '-1 year' , strtotime ( $today ) ) ;
$ayearago = date( 'Y-m-d' , $newdate );

function filter_where($where = '') {
        $where .= " AND post_date >= DATE('" . $ayearago . "')";
    return $where;
}

add_filter('posts_where', 'filter_where');

query_posts('posts_per_page=-1');
?>
<channel>
    <title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
    <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    <link><?php bloginfo_rss('url') ?></link>
    <description><?php bloginfo_rss("description") ?></description>
    <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
    <language><?php echo get_option('rss_language'); ?></language>
    <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
    <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
    <?php do_action('rss2_head'); ?>
    <?php while( have_posts()) : the_post(); ?>
    <item>

Thanks,

Ryan

Related posts

Leave a Reply

2 comments

  1. Hi @Ryan Long:

    You have two problems:

    1. You need to wrap the result of date( 'Y-m-d' , $newdate ); in a SQL DATE() function, and
    2. You define $ayearago outside of the filter_where() function so it’s not in scope within the function. You need to move the code that sets $ayearago into the filter_where() function like this:
    function yoursite_filter_where($where = '') {
      $today = date('Y-m-d');
      $newdate = strtotime ( '-1 year' , strtotime ( $today ) ) ;
      $ayearago = date( 'Y-m-d' , $newdate );
      $where .= " AND post_date >= DATE('{$ayearago}')";
      return $where;
    }
    

    Let me know if that works for you…

  2. There are many ways to do this:

    $today = getdate();
    $lastyear = $today[ 'year' ] - 1;
    
    query_posts( array(
            'posts_per_page' => -1,
            'year' => $lastyear,
        ) );
    

    or

    function your_function() {
        $today = getdate();
        $lastyear = $today['year'] - 1;
        set_query_var( 'year', $lastyear );
    }
    
    add_filter( 'pre_get_posts', 'your_function' );
    

    Also for feed, might wanna add is_feed()