1

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...

  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.

1 Answer 1

2

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.

3
  • Thanks Mike. Man am I foolish. All I had to do was pull in the $_POST data rather than get_post_meta() and issue (1) from above is taken care of. So basically what I used for that was function save_dis($data, $postarr) { $my_post_date = $_POST['_start_one_year'].'-'.$_POST['_start_one_month'].'-'.$_POST['_start_one_day'].' '.$_POST['_start_one_hour'].':'.$_POST['_start_one_min'].':00'; $data['post_date'] = $my_post_date; $data['post_date_gmt'] = date('Y-m-d H:i:s', ($newgmt)); return $data; } add_filter('wp_insert_post_data', 'save_dis', 99,2);
    – user2271
    Jan 4, 2011 at 23:50
  • Sorry about that last comment, still learning stackexchange format. Let me know if anyone wants me to post a more coherent trail of what I'm doing. Still looking for issue (2) above.
    – user2271
    Jan 5, 2011 at 0:00
  • @user2271 - Glad I could help, not need to feel foolish. We all miss things. (I just had a client ask why we couldn't do something a certain way and I could only reply "I hadn't thought of that!" :) For new to StackExchange, remember to up-vote all good answers and to select the best answer; that rewards those who helped you. Also you might consider answering your own question here since you derived a specific solution. In your answer (and in your questions) highlight your code then click the code button to indent all the code 4 spaces which causes SE to format the code like in my answer. Jan 5, 2011 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.