构建WP\\u User\\u查询时,您的思路是正确的,但您没有使用此查询的结果。请参见:
//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) ) {
//gets subscirbers to send email to
// WP_User_Query arguments
$args = array (
\'role\' => \'Subscriber\',
);
// The User Query
$user_query = new WP_User_Query( $args );
// get email addresses from user objects
$email_addresses = array();
foreach ( $user_query->results as $user ) {
$email_addresses[] = $user->user_email;
}
// build message
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = \'A post has been updated\';
$message = "A post has been updated on your website:\\n\\n";
$message .= "<a href=\'". $post_url. "\'>" .$post_title. "</a>\\n\\n";
//send email to all emails
wp_mail($email_addresses, $subject, $message );
}
我们循环所有用户并使用每个电子邮件地址构建一个数组,我们直接将此数组用作
wp_mail()
(它支持阵列)请注意,您可能需要使用第三方服务一次发送多封邮件,否则您可能会与托管提供商发生问题。看看
Mandrill. 他们有一个WordPress插件,可以很好地与
wp_mail()
作用