There are many ways you can do this. For my examples, I’m going to assume you’ve stored the emails in the post_title
field. wp_mail
takes a string or array in the to field, so you can do something like this…
<?php
$emails = get_posts(array(
'post_type' => 'your_email_storing_type',
'nopaging' => true,
));
$to_send = array();
foreach ($emails as $e) {
if (!e) {
continue;
}
$to_send[] = $e->post_title;
}
wp_mail($to_send, /* ... */);
Or you can implode
them all together (which you shouldn’t wp_mail
will take care of it for you).
<?php
$emails = get_posts(array(
'post_type' => 'your_email_storing_type',
'nopaging' => true,
));
$to_send = array();
foreach ($emails as $e) {
if (!e) {
continue;
}
$to_send[] = $e->post_title;
}
wp_mail(implode(',', $to_send), /* ... */);
Or you could go directly to wpdb
and offload the concatenation to MySQL.
<?php
global $wpdb;
$emails = $wpdb->get_var("SELECT GROUP_CONCAT(post_title SEPARATOR ',') FROM {$wpdb->posts} WHERE post_title="your_email_storing_type"");
wp_mail($emails, /* ... */);