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.
$headers
array, but where does the$headers
array come from? It's never been declared/created? Can you re-indent your code correctly?[]
operator, but it's never actually created, it's only implied