1

In my theme, I want to create the function that when a metabox value is changed, the author is notified.

Effectively, I have a custom metabox that loops through a certain level of user role (custom user role), with the <option value="<the user's ID>"

I was able to do this through changing the author with the following code:

function check_values($post_ID, $post_after, $post_before){
    if( $post_after->post_author !== $post_before->post_author ) {
        $author = $post_after->post_author; /* Post author ID. */
         $name = get_the_author_meta( 'display_name', $author );
         $email = get_the_author_meta( 'user_email', $author );

         $title = $post_after->post_title;
         $permalink = get_permalink( $ID );
         $edit = get_edit_post_link( $ID, '' );
         $to[] = sprintf( '%s <%s>', $name, $email );
         $subject = sprintf( 'Published: %s', $title );
         $message = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
         $message .= sprintf( 'View: %s', $permalink );
         $headers[] = '';
         wp_mail( $to, $subject, $message, $headers );
    }
}
add_action( 'post_updated', 'check_values', 10, 3 );

I tried adding get_post_meta( get_the_ID(), 'key_1', true ); but looking through the codex for the post_updated I couldn't see if it was possible to call $post_before->$meta_key or $post_after->$meta_key

Is there a way to do this? Or is it purely for inbuilt values?

  • There's also a way to do action after specific meta is updated see pastebin.com/y10rxawx – Samuel Elh Sep 4 '16 at 11:48
  • @SamuelElh I'm trying to fully understand your pastebin, and the codex. Does that action fire every time the post is updated? Also, would I be right to assume that it doesn't compare the values from each update? I could only see it being called every time, rather than if it needs to be. Could you please clarify the function a little for me – Ahhhhhhhhhhhhhdfgbv Sep 5 '16 at 5:17
0

Like @SamuelElh comment this is how I solved it:

add_action('updated_post_meta', 'check_meta_change', 0, 4);
function check_meta_change($meta_id, $post_id, $meta_key, $meta_value) {
    if( '<ENTER YOUR META KEY>' == $meta_key ) {
        // do stuff
    }
}

Your Answer

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

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