I'm using rilwis' Meta Box plugin (see https://github.com/rilwis/meta-box) to add custom meta boxes to a custom post type.

I then want to populate one piece of meta data based on the value of another. I'm doing this using the updated_post_meta action hook using some code similar to the following:

add_action( "updated_post_meta", "my_generate_extra_meta", 10, 4 );
add_action( "added_post_meta", "my_generate_extra_meta", 10, 4 );

function my_generate_extra_meta( $meta_id, $object_id, $meta_key, $meta_value ) {
    // Only do this for resources post type
        if (get_post_type($object_id) == 'resource') {
        // Only do this if we're saving the _my_resource_meta_1 data
        if ($meta_key == '_my_resource_meta_1') {
               $new_meta = do_some_processing_of( $meta_value );
               update_post_meta( $object_id, '_my_resource_meta_2', $new_meta );

But this seems to happen before the _my_resource_meta_2 is saved from the data submitted. So the value I save using update_post_meta gets overwritten by the submitted value (or deleted if the meta value was empty).

What I want to know is, what is the best way to prevent subsequent updates of _my_resource_meta_2 here?

The best way I've found so far is to do something like:

$_POST['_my_resource_meta_2'] = $new_meta;
$_REQUEST['_my_resource_meta_2'] = $new_meta;

at the end of the action. But this feels like a nasty hack, and I guess you still need to do the update_post_meta in case the _my_resource_meta_2 is updated first, which means you do the update twice.

Can anyone think of other solutions to this problem? Is there a better way?


  • Does _my_resource_meta_2 also updated when the resource post type is saved? or it is a unique value related only to _my_resource_meta_1? I mean do you have a meta box field for _my_resource_meta_2? – Ahmad M Dec 7 '12 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.