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
Hi @Ryan Long:
You have two problems:
date( 'Y-m-d' , $newdate );
in a SQLDATE()
function, and$ayearago
outside of thefilter_where()
function so it’s not in scope within the function. You need to move the code that sets$ayearago
into thefilter_where()
function like this:Let me know if that works for you…
There are many ways to do this:
or
Also for feed, might wanna add
is_feed()