How to send mail to subscribers using wp_mail

You were on the right track when you built your WP_User_Query, but you are not using the result of this query. See this:

 //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 );

}
  • we loop all users and build an array with each email address
  • we use this array directly as a parameter of wp_mail() (it supports arrays)

Note that you would probably need to use a third-party service to send many mails at once, or you could have problem with your hosting provider. Have a look at Mandrill. They have a WordPress plugin that works well with the wp_mail() function.