i want to send email only new post publish in wordpress admin
when i pulish anything its sending mail to user how to restric this to only new post added to send mail to user
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
if ( $new_status !== 'publish' || $old_status === 'publish' )
return;
if ( ! $post_type = get_post_type_object( $post->post_type ) )
return;
global $wpdb;
$newsletterdata = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."newsletter");
foreach ( $newsletterdata as $newsletteremailall )
{
$newsletteremailall = $newsletteremailall->email;
if(filter_var($newsletteremailall, FILTER_VALIDATE_EMAIL))
{
$subject = $post->post_title;
$message = 'Our new post is here '."n".'Post Title: ' .$post->post_title. "n"."click here to visit post: " . get_permalink( $post->ID );
wp_mail( $newsletteremailall, $subject, $message );
}
else
{
}
}
}
add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Updated code for statuses as per @Fredrik’s answer as his is more accurate.
To trigger this when publishing, you could change your first if to
if ( $new_status === 'inherit' && $old_status === 'new' )
That way, your code should trigger only when you publish a post.
EDIT:
Some notes on your code.
if ( ! $post_type = get_post_type_object( $post->post_type ) )
this will do nothing, as it’s an assignment for a variable. It means, that you will get the current post type, and assign it to $post_type and it will always pass.
What I think you’re trying to achieve, is for this to work only for a specific post type. If that’s the case, change this line to:
if ( 'my_custom_post_type' !== get_post_type_object( $post->post_type ) )
where my_custom_post_type should be the post type for which you wish to send emails for.
For this line:
$newsletteremailall = $newsletteremailall->email;
It’s a bad practice to overwrite the initial variable. Maybe name it $email instead of $newsletteremailall, because you may need it later, but you will no longer have access to it.
Method 2
If you set the first if like this, it will ONLY send you these mails when you have created a NEW post. Not when you change it between different status. And this goes for all post types, you might (as mentioned before) look at the $post_type and change that to your custom post type, otherwise this is fired regardless of post type.
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
if ( $old_status === 'new' && $new_status === 'inherit' )
if ( ! $post_type = get_post_type_object( $post->post_type ) )
return;
global $wpdb;
$newsletterdata = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."newsletter");
foreach ( $newsletterdata as $newsletteremailall )
{
$newsletteremailall = $newsletteremailall->email;
if(filter_var($newsletteremailall, FILTER_VALIDATE_EMAIL))
{
$subject = $post->post_title;
$message = 'Our new post is here '."n".'Post Title: ' .$post->post_title. "n"."click here to visit post: " . get_permalink( $post->ID );
wp_mail( $newsletteremailall, $subject, $message );
}
else
{
}
}
}
add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0