Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created custom post types that also have custom meta_boxes I've created. Currently, they save when I publish or update a post, but they don't save when I'm in draft mode making changes.

add_action('save_post', 'save_details');

function save_details(){
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
    return $post_id;
}
global $post;
if ($post->post_type == 'events') { // Check to see if Event Type.
    update_post_meta($post->ID, "event_featuring", $_POST["event_featuring"]);
    update_post_meta($post->ID, "event_time", $_POST["event_time"]);
    update_post_meta($post->ID, "event_date", $_POST["event_date"]);
    update_post_meta($post->ID, "event_end_date", $_POST["event_end_date"]);
    update_post_meta($post->ID, "event_location", $_POST["event_location"]);
    update_post_meta($post->ID, "empid", $_POST["empid"]);
    update_post_meta($post->ID, "bhs_event", $_POST["bhs_event"]);
}
}

I tried using wp_insert_post_data instead of save_post, but then I had the opposite problem. It would save on Drafts, but publishing the post no longer worked. What do I need to do differently so I can update a draft (before publishing) and it will save?

share|improve this question
add comment

2 Answers

So you first tried hooking on to wp_insert_post_data and could save the meta data when saving Drafts but not when publishing. Then you tried hooking on to save_post and could save meta data when publishing but not when saving Drafts.

The easiest solution would be to hook on to both.

add_action('save_post', 'save_details');
add_action('wp_insert_post_data', 'save_details');

Edit

Both save_post and wp_insert_post_data are called at the same time and pass two parameters to callback functions. In the source, it looks like this:

do_action('save_post', $post_ID, $post);
do_action('wp_insert_post', $post_ID, $post);

Bbut your function isn't accepting any parameters. This will make return $post_ID fail and will likely cause other issues as well.

You should have:

function save_details( $post_ID, $post ) {
    ...
}

And your hook should be:

add_action( 'save_post', 'save_details', 10, 2 );

This will pass both $post_ID and $post into your function and should make things run a bit more smoothly.

share|improve this answer
    
Yeah, that would make sense but with both it acts like it did with just wp_insert_post_data. It saves it, but won't publish. –  rmlumley Apr 6 '11 at 20:27
    
Please see my revision. –  EAMann Apr 6 '11 at 20:36
    
Thanks. I'm still having the same issue even with those changes. In fact, I'm not even able to get it to publish. It works fine (as posted above) when I publish or update a post. It just will not save those fields when I'm working within a draft. –  rmlumley Apr 6 '11 at 20:54
    
With all of that said, everything you wrote above certainly makes sense and I appreciate the help. For some reason though, it isn't working. –  rmlumley Apr 6 '11 at 20:58
    
Something else is probably going on ... but without seeing the full code I can't really diagnose it. And since this isn't a fix-my-specific-site-issue forum, you'd probably have better luck with deep troubleshooting over in the support forums. –  EAMann Apr 6 '11 at 21:11
add comment

I was having a similar problem which I think was triggered by upgrading wordpress to 3.1 although I did not get a chance to revert back to verify. I changed my code to pass in the $post_id and $post as above taking care to pass parameter 1,2 rather than 10, 2 as above and this fixed the problem. adding a hook to wp_insert_post did not work for me and gave me errors when adding a new custom post.

Hope this help. My first response so sorry if I'm unclear.

share|improve this answer
1  
You may have the answer but this is not very clear, as you suspected. Can you provide, or recreate, the code that worked for you? –  s_ha_dum Nov 22 '12 at 14:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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