我们可以利用add_term_relationship
检查当前职位是否已分配为popular
. add_term_relationship
在插入术语之前激发。
我还认为你使用了错误的钩子来发送邮件。added_term_relationship
在进行任何错误检查之前很早激发。之后仍可能遇到故障added_term_relationship
这意味着术语插入将不会成功,并将返回错误。set_object_terms
是一个更好的钩子,因为它只有在成功插入术语时才会激发。
/**
* Fires immediately before an object-term relationship is added.
*
* @since 2.9.0
*
* @param int $object_id Object ID.
* @param int $tt_id Term taxonomy ID.
*/
do_action( \'add_term_relationship\', $object_id, $tt_id );
$wpdb->insert( $wpdb->term_relationships, array( \'object_id\' => $object_id, \'term_taxonomy_id\' => $tt_id ) );
/**
* Fires immediately after an object-term relationship is added.
*
* @since 2.9.0
*
* @param int $object_id Object ID.
* @param int $tt_id Term taxonomy ID.
*/
do_action( \'added_term_relationship\', $object_id, $tt_id );
我们可以尝试以下方法(
NOTE: 这是未经测试的
add_action( \'add_term_relationship\', function ( $object_id, $tt_id )
{
// Check if the post is already in the popular term, if so, bail
if ( has_term( \'popular\', \'song_category\', $object_id ) )
return;
// Check if our post is going to be added as popular, if not, bail
if ( 1 !== $tt_id ) // Set the correct ID for popular term
return;
/**
* We are now sure that our post is not yet popular, and we will be
* making it popular, so lets continue
*
* We will be sending a mail when the term is inserted. We will use the
* set_object_terms hook as it fires on successful insertion of the term
*/
add_action( \'set_object_terms\', function ( $object_id )
{
$post = get_post( $object_id );
$author = get_userdata( $post->post_author );
$terms = get_the_terms( $post->ID, \'song_category\' );
$email = get_post_meta( $post->ID, \'custom_email\', true );
$message = \'Hi \' . $author->display_name;
wp_mail( $email, "Your song is now in the popular section!", $message );
});
}, 10, 2 );
编辑确保更改
1
在里面
1 !== $tt_id
到你学期的确切ID。我还刚刚将您的代码复制并粘贴到我的代码中,所以我不确定这是您的完整代码还是按原样工作
编辑2如果您不知道流行术语的ID,可以使用以下选项(我接受popular
ids术语slug)$term_object = get_term_by( \'slug\', \'popular\', \'song_category\' );
$term_id = $term_object->term_id;
让我们重写代码以合并add_action( \'add_term_relationship\', function ( $object_id, $tt_id )
{
// Check if the post is already in the popular term, if so, bail
if ( has_term( \'popular\', \'song_category\', $object_id ) )
return;
// Get the term ID of the popular term. We will get the term object by term slug.
$term_object = get_term_by( \'slug\', \'popular\', \'song_category\' );
$term_id = $term_object->term_id;
// Check if our post is going to be added as popular, if not, bail
if ( $term_id != $tt_id ) // Set the correct ID for popular term
return;
/**
* We are now sure that our post is not yet popular, and we will be
* making it popular, so lets continue
*
* We will be sending a mail when the term is inserted. We will use the
* set_object_terms hook as it fires on successful insertion of the term
*/
add_action( \'set_object_terms\', function ( $object_id )
{
$post = get_post( $object_id );
$author = get_userdata( $post->post_author );
$terms = get_the_terms( $post->ID, \'song_category\' );
$email = get_post_meta( $post->ID, \'custom_email\', true );
$message = \'Hi \' . $author->display_name;
wp_mail( $email, "Your song is now in the popular section!", $message );
});
}, 10, 2 );