Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Stop sending email everytime I updates my post

You need to use {old_status}to{new_status} hook. And use draft and publish statuses. This hook will only be executed when your post will change status from draft to publish.

function wpse_288250_send_email( $post ) {

    $post_id = $post->ID;

    if( wp_is_post_revision( $post_id ) ) {
        return;
    }

    $post_url = get_permalink( $post_id );
    $subject="Your post is published!";

    $message = "Testing!";
    $message .= "<a href="". $post_url. "">Click here to view</a>";

    $email = get_post_meta($post_id, 'author_email', true ); 

    wp_mail($email, $subject, $message );
}

add_action(  'draft_to_publish',  'wpse_288250_send_email' );

Keep in mind if you change your post status back to draft and again to publish e-mail will be send again. To prevent this you can update post meta which will tell you if e-mail was already sent.

function wpse_288250_send_email_once( $post ) {

    $post_id = $post->ID;

    if( wp_is_post_revision( $post_id ) ) {
        return;
    }

    $email_sent = get_post_meta( $post_id, 'email_sent' );

    if( $email_sent ) {
        return;
    }

    $post_url = get_permalink( $post_id );
    $subject="Your post is published!";

    $message = "Testing!";
    $message .= "<a href="". $post_url. "">Click here to view</a>";

    $email = get_post_meta($post_id, 'author_email', true ); 

    wp_mail($email, $subject, $message );
    update_post_meta( $post_id, 'email_sent', true );
}

add_action(  'draft_to_publish',  'wpse_288250_send_email_once' );

Related Posts:

  1. Custom post meta field effect on the performance on the post
  2. How to get custom post meta using REST API
  3. Difference between meta keys with _ and without _ [duplicate]
  4. Orderby meta_value only returns posts that have existing meta_key
  5. What is “meta_input” parameter in wp_insert_post() used for?
  6. How to enable revisions for post meta data?
  7. Using get_post_meta with new_to_publish
  8. Can I count the number of users matching a value in a multiple value key?
  9. Auto sort the wp-admin post list by a meta key
  10. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  11. passing argument to get_template_part() or a better way to code
  12. Get updated meta data after save_post hook
  13. Create meta boxes that don’t show in custom fields
  14. Ordering posts by anniversary using only day and month
  15. How to break meta values into different items and avoid duplicates?
  16. ajax delete value from custom field array
  17. How to use pagination with get_post_meta
  18. Copying Custom Meta Values from existing post to a duplicate post
  19. Move value of one custom field to another
  20. Displaying posts with only upcoming dates according their custom field date value
  21. Custom fields: In what order are they saved into the DB?
  22. Get a post_id where meta_value equals something in a serialized meta_value field
  23. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  24. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  25. How to save a ToggleControl value in a meta field?
  26. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  27. How to display Meta Field Value?
  28. MySQL query to set wp_postmeta using term_taxonomy_id value
  29. How to Validate Post Meta type/extension (Video File Image File etc)
  30. Custom Meta Box not Saving in Posts with Gutenberg Editor
  31. How can I sort homepage by a meta value?
  32. How do I use wp_query for WordPress search?
  33. Nav Menu – Add class based on meta keys
  34. How to wrap meta values seperated by comma in ? [closed]
  35. Bulk remove post meta
  36. How to create html block to display extra information on woocommerce single product page
  37. Unique Post Meta Values
  38. Create Multiple File Upload Metabox in WordPress
  39. Run a check for multiple meta key values
  40. WordPress Rest API to call page data associate with custom meta
  41. Hide custom meta data if empty
  42. How to create a shortcode to print specific values stored in a post meta array?
  43. Add custom field to all posts in specific post_type
  44. Button inside Custom Meta Box triggering the Update Button
  45. How to load an assets based on custom field value?
  46. How can you include custom post meta in search without calling each key?
  47. Can I access a post meta field before the loop?
  48. wrap text around custom fields array
  49. How can I modify RSS item titles to be either the title or a custom meta field?
  50. Avoiding ACF get_field and returning to core WordPress function
  51. Allow only one post with specific meta value
  52. Get all the posts where meta field with multiple choice has several values checked
  53. wordpress custom loop ascending descending posts by custom field
  54. Move Title and the Content WYSIWYG editor position
  55. How to display childrens custom fields?
  56. how to display custom fields of post on a web page
  57. Hide custom fields when empty
  58. How do i get the attached images with custom value checked?
  59. Save all the post tags inside a custom field
  60. How to fill custom fields with brackets in their key with add_post_meta()?
  61. How add multiple wp_editor_box to new post
  62. Update post meta custom field using block editor
  63. Get custom fields without _edit_last, _edit_lock, _wp_page_template and _visual-subtitle
  64. Display posts with empty custom field
  65. Get YouTube video id from url in a custom field
  66. Related query shows same image in loop
  67. Why am I getting a “Call to member function format() on a non-object” error?
  68. Can’t Output get_post_meta?
  69. put saved metabox values back into fields and then display on the front end
  70. Modify custom field from front end
  71. Footnotes in custom fields
  72. How to get custom image field of specific post id
  73. Create a new custom field for all posts based on current custom field
  74. wordpress get_post_meta / the_meta to output custom field value
  75. Custom Query: query by post custom meta data
  76. get_post_custom_values problem, please help
  77. update_post_meta not working well
  78. Custom meta fields and meta keys
  79. Set class if a meta value is set within post archive
  80. How to add a post’s view count into the WordPress API response
  81. Can’t set custom meta fields for a post
  82. Allow HTML in Custom Metabox area
  83. Custom field metabox not showing in back-end
  84. searching by keywords in post’s metas or pagination links problem
  85. update meta field value after
  86. Query Multiple meta
  87. Query Posts based on custom field value
  88. Display Data From This Custom Media Upload Meta Box?
  89. Query postmeta values, and return multiple post_titles for common meta value
  90. When post is updated, custom metadata in text area field is overwritten
  91. Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
  92. Custom Field: Display only if a specific key is selected outside the loop
  93. When editing a post with a custom meta box the values aren’t displaying correctly
  94. Hard Define Custom Field Value
  95. add_post_meta insert null value into Database
  96. Retrieving custom field as shortcode
  97. save all acf options in one meta_value [closed]
  98. WP Query Args – search by meta_key or title
  99. get Custom field label (select/dropdown) on front end
  100. Block Editor – Meta values not saved, meta changes to empty array on update
Categories custom-field Tags custom-field, post-meta, wp-mail
Insert sometext after first h3 in content
Show ACF to front end custom post [closed]

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress