Publish Your Posts Later On Your Website

I explained quite a while ago about publishing a feed with a certain delay. Of course you can do this also for your frontend. Thus, the content will be published to the reader only after a certain period of time. A little script as an example will show you how.

The function can be expanded, for example, with queries on rights of the logged users or various other requirements.

In the first snippet the output on the frontend is always filtered, only your backend is_admin() and your feed is_feed() keeps untouched. The filter is placed in the example at 15 minutes and these are aligned with the setting of WordPress, so that the time zone will be considered. The 15 minutes will be converted into seconds and subtracted from the value.

function publish_later_on_frontend( $where ) {

	if ( is_admin() || is_preview() || is_feed() )
		return $where;

	$offset =  15; // minutes
	$offset = ( get_option( 'gmt_offset' ) * 3600 ) - ( $offset * 60 ); // gmt offset of WP options minus custom time
	$where .= " AND post_date < '" . gmdate( 'Y-m-d H:i:s', ( time() + $offset ) ) . "'";

	return $where;
add_filter( 'posts_where', 'publish_later_on_frontend' );

Another brief example will allow all users logged, with minimum rights to read current_user_can('read') – so subscribers, to read the contents. All other visitors will get the content presented 10 days later.

function publish_later_on_frontend( $where ) {

	if ( is_admin() || is_preview() || is_feed() || current_user_can('read') )
		return $where;

	$offset =  10; // days
	$offset = ( get_option( 'gmt_offset' ) * 3600 ) - ( $offset * 24 * 60 * 60 ); // gmt offset of WP options minus custom time
	$where .= " AND post_date < '" . gmdate( 'Y-m-d H:i:s', ( time() + $offset ) ) . "'";

	return $where;
add_filter( 'posts_where', 'publish_later_on_frontend' );

Alternatives with SQL-Statement:

// with INTERVAL 
$where .= " AND post_date < NOW() - INTERVAL 5 MINUTE"; // DAY, HOUR
// with strtotime()
$where .= " AND post_date < FROM_UNIXTIME(" . strtotime("-5 day"). ")";

Well - any other ideas? Then let us know in our comment area. I'm happy to see any improvements to the code, especially in the area of calculation with time.





5 responses to “Publish Your Posts Later On Your Website”

  1. curtismchale Avatar

    I’m unclear as to why you wouldn’t just use the built in scheduling feature of WordPress in the first example?

  2. zaryl Avatar

    I’m not sure what is wrong but usually when I set the schedule for the post, it will not be published. I had to do it manually after all.

    hope that these codes can help solve the problem

  3. Frank Avatar

    It is only an example, you combinate the source with all ideas, maybe an publish for the first time only on logged in users other what you think. Bu the first one of source is the baasics for doing; maybe publish a little bid latter and only logged in users see the content and can read.

  4. Bambo Avatar

    Would it be a correct observation that normal preview does not work?

    I changed

    if ( is_admin() || is_feed() )

    if ( is_admin() || is_feed() || is_preview() )
    and preview works again.
    Seems to work fine and snippet is even more useful.

  5. Frank Avatar

    @Bambo: many thanks, right -important conditional tag, but forgotten