How to Include Future Posts in Your WordPress RSS Feed

Home » Snippets » How to Include Future Posts in Your WordPress RSS Feed
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you interested in including future posts in your WordPress site’s RSS feed? This is a unique feature that can allow your subscribers to get a sneak peek into what’s to come or ensure that scheduled content is included in your feed. However, doing this requires some alterations to your RSS setup. In this article, we’ll show you step-by-step how to include future posts in your RSS feed, enhancing the browsing experience for your regular readers and keeping them engaged with future content offerings.

					function wpturbo_include_future_posts_in_rss($query) {
  if ($query->is_feed) { 
    $query->set('post_status', array('publish', 'future'));
  }
  return $query;
}

add_filter('pre_get_posts', 'wpturbo_include_future_posts_in_rss');
				

The PHP code snippet starts by defining a function called wpturbo_include_future_posts_in_rss(), which takes one parameter, $query.

Inside this function, we evaluate a condition if ($query->is_feed). This conditional statement checks if the query is for a RSS feed. The $query->is_feed is a WordPress parameter that checks whether the query is in a feed context.

If the conditional statement returns true, meaning we are indeed dealing with a feed, we reach the code $query->set('post_status', array('publish', 'future'));. Here is where we modify the parameters of the query.

The $query->set() function is a WordPress function used to change the query variables. This function takes two parameters. The first parameter is ‘post_status’ which is a query variable that WordPress uses to determine what status of posts should be used. The second parameter is an array containing the two statuses ‘publish’ and ‘future’.

‘publish’ means that the post is publicly viewable, while ‘future’ means that the post is scheduled to be published on a future date. Normally, future posts wouldn’t be included in an RSS feed, but with this setup they will be.

Lastly, we return the modified $query to make sure that our changes are correctly applied to the WordPress query.

The final line add_filter('pre_get_posts', 'wpturbo_include_future_posts_in_rss'); hooks our function into the ‘pre_get_posts’ filter. ‘pre_get_posts’ is a WordPress hook that enables modification of the query before the actual SQL query is run, hence why it’s an ideal place to implement our function.

The ‘add_filter’ function links our wpturbo_include_future_posts_in_rss() function to the ‘pre_get_posts’ hook, meaning any RSS feed queries would now include future posts as per our custom function. This would be especially useful in cases where you might want to give RSS subscribers a sneak peek of your upcoming posts.

Register an account to save your snippets or go Pro to get more features.