How do I save metadata for a specific custom post type only?

function save_my_metadata($ID = false, $post = false)
{
    if($post->post_type != 'your_post_type')
        return;
    update_post_meta($ID, 'my_metadata', $_POST['my_metadata']);
}

That should work. Just replace ‘your_post_type’ with the name of the post type. Also, little known fact: the ‘save_post’ hook passes the post’s ID as an argument.

EDIT

I updated the function to reflect Jan’s comment. Thanks Jan!

Leave a Comment