最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Include only recents custom posts in WP rss feed

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Apr 20, 2019 at 17:28 Fayaz 9,0172 gold badges33 silver badges51 bronze badges asked Apr 20, 2019 at 13:44 Robert ZalinyanRobert Zalinyan 191 bronze badge 2
  • 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 Commented Apr 20, 2019 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 Commented May 3, 2019 at 20:24
Add a comment  | 

1 Answer 1

Reset to default 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.
发布评论

评论列表(0)

  1. 暂无评论