Are you looking to do something after the post is published which was drafted previously? This can be useful if you’re accepting posts from the frontend by creating a frontend form using ACF which you’re keeping in draft status because you’re reviewing. Now, you’ve reviewed it and published it. But you want to do another thing as well – e.g. send an email to the post author telling the post is published.

The appropriate hook is “transition_post_status”

add_action( 'transition_post_status', 'sa_send_email' ), 10, 3 );

	/**
	 * Send an email when the submitted post status changes from "draft" to "publish". i.e. when admin approves the post.
	 *
	 * @param string $new_status New Status.
	 * @param string $old_status Old Status.
	 * @param object $post       The WP_Post Object.
	 */
	function sa_send_email( $new_status, $old_status, $post ) {

		if ( $old_status === 'draft' && $new_status === 'publish' && $post->post_type === 'site' ) {

			$author_id           = $post->post_author;
			$author_display_name = get_the_author_meta( 'display_name', $author_id );

			if ( empty( $author_display_name ) ) {
				$author_display_name = get_the_author_meta( 'user_login', $author_id );
			}

			$author_email = get_the_author_meta( 'user_email', $author_id );
			$post_title   = $post->post_title;
			$post_link    = get_permalink( $post->ID );

			$subject = esc_html__( 'Review Outcome for Your Post Submission', 'text-domain' );

			$message = "Dear {$author_display_name},

            I hope this email finds you well. We wanted to update you on the status of your post submission for {$post_title} on our website.
            
            After a thorough review by our team, we have the following update:
            
            We are pleased to inform you that your post, {$post_title}, has been approved and is now live on our platform's listings. You can view your listing here: {$post_link}.
            
            Congratulations! Your post is now visible to potential buyers, and we look forward to helping you find the right match.

            Regards,";



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

Take a look at {$new_status}_{$post->post_type} hook as well.

I hope you found it helpful!

WP Hook after the post status is changed
Tagged on:

Sanjeev Aryal

Don't bury your thoughts, put your vision into reality ~ Bob Marley.

Leave a Reply

Your email address will not be published. Required fields are marked *

× WhatsApp