当特定类别中的帖子获得另一个类别时,是否防止功能再次触发?

时间:2016-01-31 作者:JediTricks007

我创建了一个功能,可以在自定义帖子类型的热门类别中添加帖子时向作者发送电子邮件。

这是可行的,但有一个主要问题。如果帖子是in the popular category gets added to a new category while still in the popular category 它发出another email.

我确实明白为什么会发生这种情况。这是因为添加了新的长期关系。我还没有找到处理这个问题的方法,已经尝试了几天了。所以我想最好问问这里的社区。

有没有办法阻止此电子邮件再次发送?

这是一个简化版本

<?php

/**
* Sends out an email when song goes into popular category
*
* @param int $post_id of the song
*/


function emailNotificationSongPopular( $post_id ) {

  if ( has_term( \'popular\', \'song_category\' ) ) {

    $post    = get_post( $post_id );
    $author  = get_userdata( $post->post_author );
    $terms   = get_the_terms( $post->ID, \'song_category\' );
    $email   = get_post_meta( get_the_ID(), \'custom_email\', true );
    $message = \'Hi \' . $author->display_name;

    wp_mail( $email, "Your song is now in the popular section!", $message );

  }
}
add_action( \'added_term_relationship\', \'emailNotificationSongPopular\' );

1 个回复
SO网友:Pieter Goosen

我们可以利用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 );

相关推荐

在WP搜索表单中是否有类似`wp_Dropdown_Categories()`的输入复选框选项?

我有自定义的帖子类型,当前正在使用wp_dropdown_categories() 在WP搜索表单中的功能,以便人们能够按薪资级别和工作类型进行筛选。是否有一个等效函数,以便用户可以使用输入复选框元素而不是<select> 元素,以便他们在进行搜索时可以选择多个工作类型或薪资水平?我似乎在开发者文档中找不到任何东西。我正在寻找的是允许复选框替换使用下面代码中的select选项的下拉元素的东西。<form method="get" action="<?p