WordPress:wp_mail函数只能在foreach循环中运行一次

时间:2017-10-05 06:41:24

标签: php wordpress

我正在运行一个带有删除旧事件功能的cron(基于自定义字段)。 该函数可以查找和删除过去的所有事件。 到目前为止一切都很好......

现在我想向该活动的作者发送一封电子邮件,该活动已被删除。我在foreach中使用额外的wp_mail函数执行此操作。

现在的问题是,只有最后一个旧事件被删除。也许这与foreach中的邮件变量($post$author,...)有关吗?

我在这里读到了一些关于这个问题的内容,但我不明白:wp_mail() in a loop, only sending to last address

这是我的代码:

function get_delete_old_events() {

    $past_query = date('Y-m-d', strtotime('-1 day'));

    // Set our query arguments
    $args = [
        'fields'         => 'ids', // Only get post ID's to improve performance
        'post_type'      => 'event', // Post type
        'posts_per_page' => -1,
        'meta_query'     => [
            [
                'key'     => 'gid_22', // Replace this with the event end date meta key.
                'value'   => $past_query,
                'compare' => '<='
            ]
        ]
      ];
    $q = get_posts( $args );

    // Check if we have posts to delete, if not, return false
    if ( !$q )
        return false;

    // OK, we have posts to delete, lets delete them
    foreach ( $q as $id )

        /* start e-mail  */
            $headers[] = 'From: SITENAME <hello@domain.com>';

            $post       = get_post($id);
            $author     = get_userdata($post->post_author);
            $subject    = "SUBJECT: ".$post->post_title."";

            $message = "THE MESSAGE BODY";

            wp_mail($author->user_email, $subject, $message, $headers);
        /* end e-mail */

        wp_trash_post( $id );
}

// expired_post_delete hook fires when the Cron is executed
add_action( 'old_event_delete', 'get_delete_old_events' );


// Add function to register event to wp
add_action( 'wp', 'register_daily_events_delete_event');

function register_daily_events_delete_event() {
    // Make sure this event hasn't been scheduled
    if( !wp_next_scheduled( 'old_event_delete' ) ) {
        // Schedule the event
        wp_schedule_event( time(), 'hourly', 'old_event_delete' );
    }
}

2 个答案:

答案 0 :(得分:1)

wp_mail函数返回bool(参见Wordpress Documentation here)。因此,它退出函数,因为它返回第一次迭代中的值。

只需在wp_mail

之前添加变量即可
 $is_sent = wp_mail($author->user_email, $subject, $message, $headers);

它应该工作。

答案 1 :(得分:1)

尝试以下代码。

function get_delete_old_events() {
    $past_query = date('Y-m-d', strtotime('-1 day'));
    // WP_Query arguments
    $args = array(
        'fields'         => 'ids', // Only get post ID's to improve performance
        'post_type'      => array( 'event' ), //post type
        'posts_per_page' => '-1',//fetch all posts,
        'meta_query'     =>array(
                                    'relation'  => 'AND',
                                    array(
                                            'key' => 'gid_22',
                                            'value' =>  $past_query,
                                            'compare'   => '<='
                                          )
                                  )
        );

    // The Query
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            // do something
            $headers[] = 'From: SITENAME <hello@domain.com>';

            $postid     = get_the_ID();
            $post       = get_post($postid);
            $author     = get_userdata($post->post_author);
            $subject    = "SUBJECT: ".get_the_title()."";

            $message = "THE MESSAGE BODY";

            wp_mail($author->user_email, $subject, $message, $headers);
            wp_trash_post( $id );
        }
    } else {
        // no posts found
        return false;

    }

    // Restore original Post Data
    wp_reset_postdata();

}