0

I'm having troubles trying to do a hook when post published. I think that the problem is that my WP-Metaboxer plugin creates the meta after publishing post, and I cannot capture the value.

Here is an example:

function post_published_coins_update( $ID, $post ) {    
        $tipo = get_post_meta( $post->ID, 'tipo');

        $coins_value = $tipo;

        add_user_meta($post->post_author, 'coins', $coins_value);
    }
add_action('publish_post', 'post_published_coins_update',  10, 2);

I've created a metabox that create a post_meta called "tipo", but the hook doesn't access to it, is like if the hook starts before than the meta creates.

Any idea?

Thanks!

1 Answer 1

3

There is a dynamic hook right before any new metadata is added:

do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );

It would be add_post_meta for posts accordingly.

Note that you might to tinker some to catch this precisely in the case of new post being created, for example hook into publish_post first, note the ID, and add to this hook from there.

2
  • Hummm... I think update_metatype_meta could be better: codex.wordpress.org/Plugin_API/Action_Reference/…
    – EirisDG
    Commented May 9, 2015 at 17:18
  • Updates fire when updating existing metadata, that won't catch meta being set for the first time (as far as I remember).
    – Rarst
    Commented May 9, 2015 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.