0

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

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.

| improve this answer | |
  • Okey i will change it thanks @Petar Petrov sir – jykmhar123 Jul 29 at 8:28
  • No problem @jykmhar123. If this answer helped answer your question, please mark it as such. If you have anymore questions, feel free to comment here :) – Petar Petrov Jul 29 at 8:38
  • When I move post to trash its sending me mail – jykmhar123 Jul 29 at 8:40
  • Yes, that was the issue I mentioned. I've updated my answer to use @Fredrik's statuses, as I was not aware of the new status in combination with inherit. If possible, you could flag his answer as the better one. – Petar Petrov Jul 29 at 9:51
0

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 );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.