I would like to send email on custom post update, for which I saved email in a different table ‘wp_offers’, there are multiple entries for every single post.
What I am trying to is using implode() function to send email on all stored email address, but implode is not working:
function send_emails_on_new_event( $post ) {
$id = get_the_ID( $post->ID );
global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "offers";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" . "WHERE post_id=" . $id);
foreach ($retrieve_data as $retrieved_data){
$email = implode(",",$retrieved_data->email);
}
$emails = $email;
$headers = 'From: Name <[email protected]>';
$title = wp_strip_all_tags( get_the_title( $post->ID ) );
$message = 'New post created' . " " . $email;
if ( get_post_type( $post->ID ) === 'property' ) {
wp_mail( $emails, 'New Post', $message, $headers );
}
}
add_action( 'pre_post_update', 'send_emails_on_new_event' );
I displayed all emails using this code but I want them one by one separated with comma ,
This code worked and displayed all emails in <li>
:
<?php
$id = $post->ID;
global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "offers";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<ul>
<?php foreach ($retrieve_data as $retrieved_data){ ?>
<li> <?php echo $retrieved_data->email;?></li>
<?php
}
?>
</ul>
Update:
It worked with this solution:
$emailsData = [];
foreach ($retrieve_data as $retrieved_data){
$emailsData[] = $retrieved_data->email;
} // foreach
$emails = implode(",",$emailsData);
Thanks
But when change query from:
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
TO:
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" . "WHERE post_id=" . $id);
Why it’s not working?
3
Answers
You do this:
It doesn’t work because you’re giving implode() a string, not an arry.
Try making an array of the email strings, then imploding that.
This is a common code pattern for turning some column from a SQL resultset into a comma-separated list.
The problem is that implode expects array
What you could do is to collect the emails in one array and then implode. Something like this might help:
The confusion lies in wanting to implode something that is not an array:
By the way these are too generic for variable names: they do not describe the data at all. Unless the object you are building is very generic, this is poor practice. I will rename them so that the answer begins to become clear. I am assuming this is a user object, but it can be anything else:
The
implode()
fails because the argument for the implode is not an associative array, but a simple string$retrieved_data->email
.Additionally, if you want to use the wp_mail function, you do not need to implode it if you already have it in an array:
https://developer.wordpress.org/reference/functions/wp_mail/