1

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

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

2
  • 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.) Commented Feb 4, 2011 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
    Commented Feb 4, 2011 at 16:31

2 Answers 2

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

4
  • 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
    Commented Feb 4, 2011 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. Commented Feb 4, 2011 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
    Commented Feb 4, 2011 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/… Commented Feb 4, 2011 at 17:32
1

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

2
  • 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
    Commented Feb 4, 2011 at 16:19
  • Nice suggestion. One thing is it unfortunately doesn't handle is a "rolling year ago" requirement. Commented Feb 4, 2011 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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