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 am working on a theme which contains a custom post type with custom fields.

Everything is working perfectly except the when a new custom post is added, the custom fields for the first custom post and first one only are wiped.

function save_details(){  
    global $post;  
    update_post_meta($post->ID, "testimonyname", $_POST["testimonyname"]);
}  
add_action('save_post', 'save_details'); 

This is what i am using to save the custom field data

share|improve this question
    
Where exactly are you calling the save_details function? Or is it hooked into something? Do you mean you're using custom fields? Or did you create a custom meta box for your post type? –  chrisguitarguy Sep 28 '11 at 13:21
    
sorry, i added a meta box for the post type. I have updated the code with the function call –  Ashley Staggs Sep 28 '11 at 13:24

3 Answers 3

First of all, save_post has an argument $post_id, so there's no need to global $post. In some cases, $post might not actually be set (anymore) when you do this, which is probably why it breaks. Second, you should probably check whether you're storing a revision or not (see below). So your code should look like this:

function save_details( $post_id ){  
    if ( wp_is_post_revision( $post_id ) )
        $post_id = wp_is_post_revision( $post_id );

    update_post_meta( $post_id, "testimonyname", $_POST["testimonyname"] );
}  
add_action('save_post', 'save_details', 10, 1); 
share|improve this answer
    
From my tests this answer works perfectly. Thanks –  Ashley Staggs Sep 28 '11 at 14:24
    
Can i ask what the 10,1 is for in the save_post action? –  Ashley Staggs Sep 28 '11 at 14:26
    
The 10 is the priority, the 1 is the number of variables that is passed on, in this case 1 :) –  Joost de Valk Sep 28 '11 at 14:33
    
Sorry, but after more extensive testing it didn't work –  Ashley Staggs Oct 9 '11 at 18:46
up vote 0 down vote accepted

the problem was worse than I thought.

Here is the solution: Add New Post (Custom Post Type) Deletes 1 Post's Custom Meta Data

share|improve this answer

The above answer by Joost didn't work for me. Instead I found changing the action from save_post to post_updated solved my problem.

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.