How to disable lastBuildDate
for rss feed and include only the posts created recently (ex. 3 days before now)?
If there is no post, let the feed be empty. I wants to show only recent posts in rss feed.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityHow to disable lastBuildDate
for rss feed and include only the posts created recently (ex. 3 days before now)?
If there is no post, let the feed be empty. I wants to show only recent posts in rss feed.
lastBuildDate
:lastBuildDate
should not be removed from the rss feed, it may cause rss feed validation error/warning.
However, it's possible to filter the date using get_lastpostmodified
filter hook.
The rules for filtering feed
content listing is the same as the rules for filtering any content listing in WordPress, the only difference is that you'll have to use the WP_Query::is_feed check.
So in most cases, you'll have to use WP_Query
accompanied by pre_get_posts
action hook and then use the CODE in a custom plugin or in the theme's functions.php
file.
Also, to filter the date, you may use the date_query
.
The CODE below lists only the posts from 3 days before now in the feed and gives an option to modify the value of lastBuildDate
(as you've asked in the question).
function wpse334869_lastBuildDate( $lastpostmodified ) {
// if you need, modify the $lastpostmodified here, before returning it
return $lastpostmodified;
}
function wpse334869_filter_feed( $query ) {
if( $query->is_feed() ) {
add_filter( 'get_lastpostmodified', 'wpse334869_lastBuildDate');
$query->set( 'date_query', array(
array(
'after' => '3 days ago'
)
) );
}
}
add_action( 'pre_get_posts', 'wpse334869_filter_feed' );
Note: This is just a sample concept CODE, so make sure you test it before using it on a live site.
Further Reading: