我有以下功能。这是基于成员资格的网站的一部分。当类型为“message”的帖子移动到发布状态时,它应该向属于该地区的所有用户(自定义用户元)发送一封电子邮件,这些用户在起草帖子时会被选中(分类法)。
如果我发送给一个小组,它似乎会起作用。如果我发送到所有地区,它会挂起,并且似乎没有消息传出。我试着把它分成小块(25)来减轻疼痛,但没有帮助。除了告诉客户一次只发送给一个组之外,还有什么想法吗?
add_action(\'publish_messages\', \'messages_notify\', 10, 3);
function messages_notify() {
global $post;
// if post already published, abort
if($post->post_status == \'publish\') {
return;
}
// get districts ticked, else abort
$districts = get_the_terms($post->ID, \'message_district\');
if(empty($districts)) return;
// query members who belong to one of the districts ticked
$the_districts = array();
$district_args = array(\'relation\' => \'OR\' );
foreach($districts as $district){
$the_districts[] = $district->name;
if ($district->name == \'Northeast\') {
$district_args[] = array(\'key\' => \'whprms_district_ne\', \'value\' => 1);
}
if ($district->name == \'North Central\') {
$district_args[] = array(\'key\' => \'whprms_district_nc\', \'value\' => 1);
}
if ($district->name == \'Southeast\') {
$district_args[] = array(\'key\' => \'whprms_district_se\', \'value\' => 1);
}
if ($district->name == \'Southwest\') {
$district_args[] = array(\'key\' => \'whprms_district_sw\', \'value\' => 1);
}
if ($district->name == \'West\') {
$district_args[] = array(\'key\' => \'whprms_district_w\', \'value\' => 1);
}
}
$district_members = get_users(
array(
\'meta_query\' => $district_args,
)
);
$attachment_args = array(\'post_type\' => \'attachment\', \'orderby\' => \'date\', \'order\' => \'ASC\', \'numberposts\' => -1, \'post_status\' => NULL, \'post_parent\' =>$post->ID);
$attachments = get_posts($attachment_args);
if ($attachments) {
$att_array = array();
foreach ($attachments as $attachment) {
$att_array[] = get_the_title($attachment->ID);
}
}
// message only users with desired role(s)
$bcc = array();
foreach($district_members as $district_member) {
if(user_can($district_member->ID, \'board_members\') || user_can($district_member->ID, \'members\')) {
$bcc[] = $district_member->user_email;
}
}
$subject = \'WHPRMS Message: \'.$post->post_title;
$message = \'New message posted by \';
if(user_can($post->post_author, \'administrator\')) {
$message .= get_the_author_meta(\'user_nicename\', $post->post_author)."\\n\\r";
}
else {
$message .= get_the_author_meta(\'first_name\', $post->post_author).\' \'.get_the_author_meta(\'last_name\', $post->post_author)."\\n\\r";
}
$message .= \'Excerpt: \'.stripslashes(substr($post->post_content, 0, 240)).\'...\'."\\n\\r";
$message .= \'Link: \'.get_permalink($post->ID)."\\n\\r";
$message .= \'District(s): \'.implode(\', \', $the_districts)."\\n\\r";
if (!empty($att_array)) {
$message .= \'Attachment(s): \'.implode(\', \', $att_array)."\\n\\r";
}
$message .= \'You are receiving this message because you belong to one of the districts mentioned above.\';
$chunked_bcc = array_chunk($bcc, 25);
foreach($chunked_bcc as $bcc_chunk){
$headers = array();
$headers[\'Bcc\'] = \'BCC: \'.implode(\', \', $bcc_chunk);
wp_mail(\'no-reply@whprms.org\', $subject, $message, $headers);
}
}