How to Delay WordPress Feed, Publish it Later: A Step-by-Step Guide

Home » Snippets » How to Delay WordPress Feed, Publish it Later: A Step-by-Step Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Scheduling content publication is a common practice among successful bloggers and site administrators, and with WordPress, you can easily apply this method to your website. Have you ever found yourself wanting to delay the publishing of your feed until a specific date or time? It’s a helpful strategy, particularly when you aim to engage with audiences in different time zones or want to maintain a consistent posting rhythm. In this article, we will guide you through the steps necessary to set up the ‘publish feed later’ function on your WordPress site. Join us as we delve into the process.

					function wpturbo_publish_feed_later($where) {
    global $wpdb;
 
    if (is_feed()) {
        $now = gmdate('Y-m-d H:i:s');
        $wait = '10';
        $device = 'MINUTE';
        $where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
    }
    return $where;
}
add_filter('posts_where', 'wpturbo_publish_feed_later');
				

The given code snippet is designed to delay the publishing of WordPress feeds by a specified time – 10 minutes in this instance.

Right at the start, a new function, wpturbo_publish_feed_later($where), is declared. It accepts an input argument $where, which is the SQL WHERE clause WordPress uses when fetching posts for a feed.

The global $wpdb; line is a reference to the WordPress database object, allowing the function to make database-level operations.

After that, the is_feed() conditional logics comes into play. It checks if the current query is for an RSS/Atom feed. If it is, it proceeds to modify the SQL WHERE clause, otherwise, it ignores the rest of the code inside the conditional and returns the WHERE clause as is.

Next, gmdate('Y-m-d H:i:s'); grabs the current time and formats it into a MySQL-compatible format. The $wait variable is set to ’10’, and the $device variable is set to ‘MINUTE’, configuring a delay time of 10 minutes.

Now, the crucial part of this function is modifying the $where clause:

$where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";

In this line, TIMESTAMPDIFF is a SQL function which returns the difference in minutes (As defined by $device) between the post’s post_date_gmt field and the current UTC/GMT time.

Hence the full expression checks where the post time in GMT minus the current GMT time is more than 10 minutes. In other words, it only includes posts that were posted more than 10 minutes ago in the query.

As the final statement, the $where clause with new changes is returned, hence modifying the query.

The function wpturbo_publish_feed_later is then hooked into the posts_where filter with add_filter('posts_where', 'wpturbo_publish_feed_later'); to ensure the altered WHERE clause is used when WordPress is generating the posts for a feed. This filter is called whenever a post is queried, thus ensuring wpturbo_publish_feed_later is running in the right context. This plays a key role in delaying your WordPress feed by 10 minutes.

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