[英]WordPress: wp_mail function only works once in foreach loop
I'm running a cron with a function to delete old events (based on a custom field). 我正在运行一个具有删除旧事件(基于自定义字段)功能的cron。 The function works find and deletes all events which lie in the past. 该函数可以查找并删除过去的所有事件。 So far so good... 到现在为止还挺好...
Now I want to send an email to the author of the event, that the event has been deleted. 现在,我想向事件的作者发送电子邮件,表明该事件已被删除。 I'm doing this with an extra wp_mail
function inside a foreach. 我通过在foreach中使用额外的wp_mail
函数来执行此操作。
The problem now is, that only the last of the old events gets deleted. 现在的问题是,只有最后的旧事件被删除。 Maybe this has somesthing to do with the variables for the mail ( $post
, $author
, ...) inside the foreach? 也许这与foreach内邮件( $post
, $author
,...)的变量有关?
I read something about that issue here but I don't understand it: wp_mail() in a loop, only sending to last address 我在这里阅读了有关该问题的内容,但我不明白: 循环中的wp_mail(),仅发送到最后一个地址
Here is my code: 这是我的代码:
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' );
}
}
wp_mail
function returns bool (See Wordpress Documentation here ). wp_mail
函数返回bool(请参阅WordPress文档 )。 So, it exits the function as it returns the value in it's first iteration. 因此,它退出函数,因为它在第一次迭代中返回值。
Just add a variable before the wp_mail 只需在wp_mail之前添加一个变量
$is_sent = wp_mail($author->user_email, $subject, $message, $headers);
It should work then. 那应该可以了。
Try below code. 尝试下面的代码。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.