1

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.

  • 2
    Hi, welcome to WPSE. Did you try anything? If so, then please edit the question and add the CODE you've tried so far. – Fayaz Apr 20 at 17:29
  • I did not write any code so far. Is there any wp function that do the rss post query in rss feed? – Robert Zalinyan May 3 at 20:24
1

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

Filtering feed content listing:

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.

Example CODE:

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:

  1. Customizing Feeds.
  2. Acceptable Date Strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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