With some help 我创建了一个WP操作,当类别发生更改时,向帖子的作者发送一封电子邮件(自定义帖子类型)。
此操作正常,但函数返回的是上一个类别,而不是当前类别。
例如,如果帖子是cat2 管理员将其更新为cat3, 发送的电子邮件表示您的帖子已更新到cat2 (更新前的类别)。
我对此完全不知所措,因为这些钩子(added_term_relationship
和deleted_term_relationships
) 应该在类别更新后启动,而不是之前。那么,他们怎么可能发送之前的类别而不是更新后的当前类别呢?
我的代码:
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@mydomain.com>\';
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\');
编辑:声明的数组。