Check if post is being published for the first time, or is an already published post being updated

I am building an add-on to an events module that checks for availability, since there wasn’t a function for that in that module. Now that I have built the logic, there are three emails I will need to send:

  • One if the post is being submitted for the first time (ie ‘New appointment’)
  • One if the post has been published by the site admin, as normal users won’t be able to publish by themselves (ie ‘Your appointment has been approved’)
  • One for if the post has been edited, AFTER it has been published already (ie ‘Your appointment has been edited’)

I have hooked into save_post and publish_post for those first two, but I want an entirely different email sent upon editing a PUBLISHED post. How can I test for whether the post is already published and this is just an edit, versus it being published for the first time?

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

Hook into edit_post to catch changes. And take a look at wp_transition_post_status() which is called on inserts and updates:

function wp_transition_post_status($new_status, $old_status, $post) {
    do_action('transition_post_status', $new_status, $old_status, $post);
    do_action("{$old_status}_to_{$new_status}", $post);
    do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
}

On publish you hook into

  • draft_to_publish,
  • pending_to_publish and
  • auto-draft_to_publish.

For edits hook into publish_to_publish.

Example

A mini plugin that notifies all authors during post publish or edit.

<?php
/**
 * Plugin Name: (#56779) Notify authors
 */
add_action( 'transition_post_status', 'wpse_56779_notify_authors', 10, 3 );
function wpse_56779_notify_authors( $new_status, $old_status, $post )
{
    if ( 'publish' !== $new_status )
        return;

    $subject = 'publish' === $old_status
        ? __( 'Edited: %s', 'your_textdomain' )
        : __( 'New post: %s', 'your_textdomain' );

    $authors = new WP_User_Query( array( 'role' => 'Author' ) );
    foreach ( $authors as $author )
    {
        wp_mail(
            $author->user_email,
            sprintf( $subject, $post->post_title ),
            $post->post_content
            // Headers
            // Attachments
        );
        // Slow down
        sleep( 5 );
    }
}

Method 2

Use this….

function save_func($ID, $post,$update) {

   if($update == false) {
     // do something if its first time publish
   } else {
     // Do something if its update
   }
}

add_action( 'save_post', 'save_func', 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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x