开发者

WordPress Metabox saves on Update or Publish, but not on Saving Draft

开发者 https://www.devze.com 2023-02-23 13:08 出处:网络
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.

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($pos开发者_如何学Got_id){
    global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
    return $post_id;
}
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. I tried calling both at the same time, same issue. What do I need to do differently so I can update a draft (before publishing) and it will save? I'm pretty sure this was working fine before I switched to 3.1.


save_post is called no matter what, be it a draft post, or a published post, so I am surprised that is not working. I do see a few oddities with your code though.

Considering you have the $post_ID, I would favor using that over the global $post. Also, I prefer the following method of detecting auto-drafts (I ripped it straight from wp-includes/post.php, I favor examining the Wordpress codebase and emulating their methods whenever possible).

Please try the following refactored code and let me know if this resolves your issue.

add_action("save_post", "save_details");

function save_details($post_ID = 0) {
    $post_ID = (int) $post_ID;
    $post_type = get_post_type( $post_ID );
    $post_status = get_post_status( $post_ID );
    if ( "events" == $post_type && "auto-draft" != $post_status ) {
        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"]);
    }
    return $post_ID;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消