3

With some help I created a WP Action to send an email to the author of a post (custom post type) when the category has been changed.

This works ok, except that the function is returning the previous category instead of the current category.

For example, if the post was cat2 and the admin updates it to cat3, the email gets sent saying that your post has been updated to cat2 (the category before the update was made).

I'm completely at a loss with this because these hooks ( added_term_relationship and deleted_term_relationships ) are supposed to fire after the category is updated, not before. So how is it possible that they're sending the previous category instead of the updated current category?

My code:

function notify_object_terms_updated( $object_id ) {
    $post_id   = get_the_ID();
    $userEmail = types_get_field_meta_value( 'your-email', $post_id );
    $newCat    = get_the_category($post_id);
    $ideaTitle = get_the_title($post_id);
    $headers   = array();
    $headers[] = 'MIME-Version: 1.0' . "\r\n";
    $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";    
    $headers[] = 'From:XYZ Continuous Improvement <[email protected]>';

    static $did = array(); // This function might fire multiple times for the same object, ensure it only runs once
        if ( ! isset( $did[ $object_id ] ) ) {
            $did[ $object_id ] = true;

     wp_mail( 
        $userEmail, 
        'Your Continuous Improvement Idea - ' . $ideaTitle, 
        '<p>The status of your Continuous Improvement idea titled "' . $ideaTitle . '" has been updated to <strong>' . $newCat[0] ->cat_name . '</strong>.</p>' . '<p>You can view the progress of your idea anytime on the <a href="http://continuous.improvemen.com/board">Continuous Improvement Board</a>.</p>',
        $headers 
        );
    }
}

add_action( 'added_term_relationship','notify_object_terms_updated');
add_action( 'deleted_term_relationships','notify_object_terms_updated');

edit: declared array.

5
  • A general note, you're adding things to the $headers array, but where does the $headers array come from? It's never been declared/created? Can you re-indent your code correctly?
    – Tom J Nowell
    Mar 17, 2015 at 17:42
  • @TomJNowell an array named $headers is created by declaration at lines 7-9.
    – user12479
    Mar 17, 2015 at 17:44
  • It's appended to using the [] operator, but it's never actually created, it's only implied
    – Tom J Nowell
    Mar 17, 2015 at 17:45
  • Are you sure that deleted term relationships is called before added term and not the other way around?
    – Tom J Nowell
    Mar 17, 2015 at 17:51
  • I've tried it both ways, no difference.
    – danzo
    Mar 17, 2015 at 18:25

1 Answer 1

0

Although the hooks are passing you the ID of the changed object in the parameter $object_id, you aren't using it and are calling get_the_ID instead which will give you the ID of the current loop item.

Try changing your uses of $post_id to $object_id instead.

Your Answer

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

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