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've added a custom post type, which is working great; I've also added two metaboxes, which appear to work well, but the content in them always disappears after a few minutes.

If anyone could help out on this I would be eternally grateful, S.

//meta box code//

add_action( 'admin_init', 'add_custom_metabox' );
add_action( 'save_post', 'save_custom_details' );

function add_custom_metabox() {
    add_meta_box( 'custom-metabox', __( 'Product Description & Ingredients' ), 'descr_custom_metabox', 'sorbets', 'normal', 'low' );
}

function descr_custom_metabox() {
    global $post;
    $proddescr = get_post_meta( $post->ID, 'proddescr', true );
    $ingredients = get_post_meta( $post->ID, 'ingredients', true );
    ?>
    <p><label for="proddescr">Product Description:<br />
        <textarea id="proddescr" name="proddescr" style="margin:0;height:7em;width:98%;" cols="45" rows="4"><?php if( $proddescr ) { echo $proddescr; } ?></textarea></label></p>
    <p><label for="ingredients">Ingredients:<br />
        <textarea id="ingredients" name="ingredients" style="margin:0;height:7em;width:98%;" cols="45" rows="4"><?php if( $ingredients ) { echo $ingredients; } ?></textarea></label></p>
<?php
}

function save_custom_details( $post_ID ) {
    global $post;   
    if( $_POST ) {
        update_post_meta( $post->ID, 'proddescr', $_POST['proddescr'] );
        update_post_meta( $post->ID, 'ingredients', $_POST['ingredients'] );
    }
}

function get_descr_ingred_box() {
    global $post;

    $proddescr = get_post_meta( $post->ID, 'proddescr', true );
    $ingredients = get_post_meta( $post->ID, 'ingredients', true );

    return array( $proddescr, $ingredients );
}
share|improve this question

2 Answers 2

up vote 3 down vote accepted

You need to check for autosave and avoid that , also check if you are in the right post type as save_post works on all posts:

function save_custom_details( $post_id ) {
    global $post;   
    //skip auto save
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    //check for you post type only
    if( $post->post_type == "sorbets" ) {
        if( isset($_POST['proddescr']) ) { update_post_meta( $post->ID, 'proddescr', $_POST['proddescr'] );}
        if( isset($_POST['ingredients']) ) { update_post_meta( $post->ID, 'ingredients', $_POST['ingredients'] );}

    }
}

WOW you guys are fast :)

share|improve this answer
    
WHOOPS! Yes, this is an important step, but the when this script is run the global $post hasn't been set up yet, that is why $post_ID is passed into the function, you do need to use $post_ID instead of getting global $post and using $post->ID. –  Drew Gourley Apr 8 '11 at 13:12
    
@Drew Gourley: Not quite true, when save_post is fired global $post is set!. and some times you need more the the post ID so globalization of $post is needed. –  Bainternet Apr 8 '11 at 13:18
    
Many thanks to you both - seems to be working great now! –  sta777 Apr 8 '11 at 13:49
    
@Bainternet: Just checked and it seems it is indeed set, but I've had similar trouble with it in the past, strange...I just figured it hadn't been set when save_post is fired on the next request. The function I wrote was much more simple and I didn't have to check for the post type. Yup, we need global $post here. –  Drew Gourley Apr 8 '11 at 14:03
    
You're referencing 3 different post ID variables in that sample, could you not just use the ID that's passed from the hook throughout the code? (like Drew has in his answer).. –  t31os Apr 9 '11 at 12:54

The only difference I see from what you are doing here and the function I use is that you are relying on global $post to set up the ID, this may be what is breaking this for you.

You'll notice the variable $post_ID is already being passed into this function:

function save_custom_details( $post_ID ) {
    global $post;   
    if( $_POST ) {
        update_post_meta( $post->ID, 'proddescr', $_POST['proddescr'] );
        update_post_meta( $post->ID, 'ingredients', $_POST['ingredients'] );
    }
}

I would suggest changing it to this:

function save_custom_details( $post_ID ) {
    if( isset($_POST) ) {
        update_post_meta( $post_ID, 'proddescr', $_POST['proddescr'] );
        update_post_meta( $post_ID, 'ingredients', $_POST['ingredients'] );
    }
}

I also switched your condtion from ( $_POST ) to ( isset($_POST) ), it is a more reliable way of getting an answer for an IF statement. Let me know if this helps. If not, I'll dig into it a little further.

share|improve this answer

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.