WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

$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');


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"
    <?php do_action('rss2_ns'); ?>
$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');

    <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(); ?>



share|improve this question
In your example code you refer to $query_string but don't include the code where you assign it nor are you explaining the context in which you are calling query_posts() (which isn't a function that people should usually call; normally get_posts() or even new WP_Query() is better.) – MikeSchinkel Feb 4 '11 at 15:44
@MikeSchinkel - Please see my edit above for more information: I changed $query_string to posts_per_page=-1 but it still does not return any data. – Ryan Lang Feb 4 '11 at 16:31
up vote 1 down vote accepted

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...

share|improve this answer
Thanks for the answer Mike. However doing what you suggest here doesn't return any posts. I also tried formatting it like this: $where .= " AND post_date >= DATE('" . $ayearago . "')"; However this also returned no results. – Ryan Lang Feb 4 '11 at 16:02
@Ryan Lang - Damn I hate when the answer is staring at me and I miss it. Fingers crossed, I think you'll find my updated code works for you. – MikeSchinkel Feb 4 '11 at 17:00
Bingo! That worked. A simple scope issue, nice catch. Thanks for the help. I also need to make sure to add the query_posts('posts_per_page=-1') to make it show all the posts instead of using the default posts per page in the admin settings. – Ryan Lang Feb 4 '11 at 17:15
@Ryan Lang - "A simple scope issue," - Yeah, it's the things I don't look for, I look for the harder stuff. Sometimes the easy stuff is just completely invisible! BTW, you might want to use get_posts() instead of query_posts() as it can be more robust. See stackoverflow.com/questions/4698576/… – MikeSchinkel Feb 4 '11 at 17:32

There are many ways to do this:

$today = getdate();
$lastyear = $today[ 'year' ] - 1;

query_posts( array(
        'posts_per_page' => -1,
        'year' => $lastyear,
    ) );


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()

share|improve this answer
actually I take that back. Both of them only return posts that were posted in the previous year (2010 in this case). What I am wanting is all post within the past year from the current date. – Ryan Lang Feb 4 '11 at 16:19
Nice suggestion. One thing is it unfortunately doesn't handle is a "rolling year ago" requirement. – MikeSchinkel Feb 4 '11 at 17:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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