Event Calendar using insert_post_data after save_post and insert_post after post_transition draft_to_schedule

OK, I’m taking on the dreaded task of developing an event calendar for my site and have gotten a bit stuck. I’m open to any and all ideas but here’s what I’m doing and what I’d like to do. I’m assuming that my main issue is the order of hooks and filters because i feel like I have a pretty good grasp on what I want to do. Also, I’d stick to wordpress hooks and filters rather than direct mysql database manipulation.

The short is…

Read More
  1. Save the first custom field and then change the current post_date to that custom field and change post_status to future, in that order, whenever a post is saved as draft or submitted for review.
  2. Take the second custom field and insert a new post with that custom field as the date of the new post and change the post_status of the new post to ‘future’ and copy the title, content to that new post from the original post. (goal is to create an identical new post for each date submitted on the original post, make the new posts childs of the original post, and schedule them all as future posts)

Now on to the long version.

First, i have a custom post_type called ‘events’. Registered for that I have some custom fields which for simplicity we’ll call _start_date_one, _end_date_one, _start_date_two, _end_date_two.

What I’d like to do is allow an ‘events’ post to be created and allow input of these fields, a title, and content. So far so good. Then when someone clicks “Save Draft” or “Submit for Review” I’d like to change the current post_date to _start_date_one and change the post_status to ‘future’. Right now I can get this to occur only after the at least the second time I save most likely because I’m using
add_action(‘save_post’) to save custom fields
and then
add_filter(‘wp_insert_post_data’) to update current post data
I realize through some trac searching that insert_post_data happens prior to saving to allow me to update the $data and $postarr with new info and then saves the custom fields which is why the above happens.

Number 2 listed above I’ve tried quite a few ways to get wp_insert_post to work but have only gotten to inserting a pending post with none of the dates correct and everything else copied.

Any help is greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. Hi @user2271:

    I recently had to resolve a situation for an Event custom post type where I could not get WordPress to let me save a future date. I wasn’t trying to be as complex as yours but the follow code resolved my issue, maybe you can use it to resolve yours:

    add_action('admin_init', 'yoursite_admin_init');
    static function yoursite_admin_init() {
      global $pagenow;
      if ($pagenow=='post.php' && isset($_POST['post_date'])) { 
        $_POST['event_date'] = date('Y-m-d H:i:s',strtotime($_POST['post_date']));
      }
    }
    add_filter('wp_insert_post_data', 'yoursite_wp_insert_post_data',10,2);
    static function yoursite_wp_insert_post_data($data,$postarr) {
      global $pagenow;
      if ($data['post_type']=='event') {
        // Saving an event, 'event_date' grabbed in admin_init
        if ($pagenow=='post.php' && isset($postarr['event_date'])) {
          $data['post_date'] = $data['post_date_gmt'] = $postarr['event_date'];
        }
      }
      return $data;
    }
    

    Basically WordPress is applying blog post logic to all posts and events should follow different logic. The code I’ve posted grabs the incoming date in the 'admin_init' hook and then reuses it in the 'wp_insert_post_data' hook after WordPress has “fixed up” the dates per blog post logic.

    Understanding this issue I think will help you determine how to fix as per your more complex workflow?

    Hope this heps.