9

I manage a blog which consists of about 25 writers. Sometimes a few of them are in the process of writing a new post at once, and they end up publishing them too close to each other.

Is there a way to prevent this from happening? Something to say "Another post just went live within the last 5 minutes. You'll need to wait 55 mins to publish yours."

  • 3
    I took the liberty of slightly modifying the Question, so it's not an explicit plugin recommendation (off-topic per the faq). If there's a plugin, someone will recommend. Have you searched for one? I think a custom solution will be necessary. Have you researched this site? – brasofilo Jun 29 '13 at 2:53
  • @brasofilo +1 ... aZn137 (is this your password?), you might want to hook into pre_save_post or save_post. Can you show us some research of what you've tried so far (aside from installing plugins). – kaiser Jun 29 '13 at 10:58
  • Yeah, I've spent some time Googling, but haven't found any plugin that would do this kinda trick. I may be able to cook up my own hook. I'll look into pre_save_post and save_post. Thanks guys. – Tam N. Jun 29 '13 at 20:17
8

Here is a very rough block of code that should get you started. What it does is look for the most recent "future" or "publish" post and if that value is less than 1 hour different from the latest scheduled post, it schedules the current post to one hour plus the "most recent" time found.

function force_time_between_posts_wpse_104677($data, $postarr) {
  global $wpdb;
  if (empty($postarr['ID'])) return $data;

  $latest = $wpdb->get_var("
    SELECT post_date
    FROM {$wpdb->posts} 
    WHERE post_status IN('future','publish') 
    AND post_type = 'post' 
    AND ID != {$postarr['ID']}
    ORDER BY post_date DESC
    LIMIT 1");
  $distance = 60; // post publication spacing in minutes
  $latest = strtotime($latest);
  $current = strtotime($data['post_date']);

  if ($latest < $current) {
    $diff = $current - $latest;
  } else { 
    $diff = 0;
  }

  if ($diff >= 0 && $diff < ($distance * 60)) {
    $new_date = $latest + ($distance * 60);
    $date = date('Y-m-d H:i:s',$new_date);
    $date_gmt = get_gmt_from_date($date);
    $data['post_date'] = $date;
    $data['post_date_gmt'] = $date_gmt;
    $data['post_status'] = 'future';
  }
  return $data;
}
add_action('wp_insert_post_data','force_time_between_posts_wpse_104677',1,2);

This does in fact force the post scheduling, and if there is already a future post the next one will be scheduled after that already scheduled post. That means that it could potentially schedule posts far into the future.

You may want to exempt certain roles from this post scheduling, or require it only for a single role, just to help keep things manageable.

| improve this answer | |
  • Thank you very much! I went ahead and pasted the snippet into my function.php file that's on my development blog. It works perfectly. First I published a post #1, then I made a second post #2 right after that. When I hit the "Publish" button, the post was set to scheduled for 60 minutes from when post #1 went live. I tested it with another post #3, setting it to go live in between post #1 and #2, it automatically set it back to 1 hour after post #2. This is perfect. Thank you very much! – Tam N. Jun 29 '13 at 21:46
  • Hmmm... so there's a bug. It always grabs the latest post that's scheduled as the base to compare. Say, post #3 is scheduled for 24 hours. When I schedule post #4 to go live in 12 hours, it will always sets the time to be 1 hour after post #3. That said, this is good for me to go on. Thanks anyway! – Tam N. Jun 29 '13 at 22:14
  • 1
    Not a bug. I explained that in the answer. It may not be the logic that you want, but it isn't a bug. If you don't do that, posts published a minute apart would still publish a minute apart, though (perhaps) delayed for an hour. – s_ha_dum Jun 29 '13 at 22:17
  • Gotcha. I suppose I only need to posts that are going live immediately, not scheduled ones. If I modified the snippet to having WHERE post_status IN('publish'), I believe it'd work better for what I need. – Tam N. Jun 29 '13 at 22:25
  • 1
    You can try that but think about what happens when several posts are published. They will all be scheduled based on the last published post-- probably the same post--, meaning that when they do publish they will still publish the same distance in time apart as they would have if you did nothing. They'd just be published a hour later. – s_ha_dum Jun 29 '13 at 22:27
0

There is a plugin in the repository named Auto Future Date which does something like this. It hasn't been updated for quite a while, but most of the code seems pretty useful.

I haven't tested it, but the screenshots make it look like you can still directly publish the post and not stick to the save_post hook automatically. Making this code work with the right hooks should do the trick.

| improve this answer | |

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.