Send automatic emails to multiple user roles when a custom field is updated in a custom post type

I’m trying to incorporate a ‘Register interest’ button into a website which when signing up will assign register the user to a user role and then when a custom field is updated in the property (CPT) it will automatically send out an email to the users who are assigned to a role letting them know a property is now available.

I have many properties in the custom post type which have a relationship to another custom post type ‘court’ so I’ve setup different user roles. If a user wants to be emailed about more than one courts properties then they will be assigned to multiple roles.

I thought by using the plugin, ‘Better notifications for WordPress’ with some of its premium add-ons would do the trick but this doesn’t let me set a conditional rule on a custom field and only works if any property status has been published.

I’ve also tried setting up Mailchimp and collecting different emails on the site which would go into separate lists of the courts but I can’t find anything which then allows me to setup a automatic email to go out these lists.

I did read about doing this manually through the wp_mail function following a tutorial on Smashing magazine, https://www.smashingmagazine.com/2015/05/building-wordpress-notification-system/ but reading the comments it talks the risks of being blacklisted.

Does anyone know of any solutions / plugins which could help me build this in?

1 Answer
1

This code will allow you to call a custom function when your meta key is updated.

add_action('updated_postmeta','my_meta_update', 10, 4);

function my_meta_update( $meta_id, $object_id, $meta_key, $meta_value ) {

    if($meta_key === 'my_meta_key') {
        // trigger email(s)
        // wp_mail( $to, $subject, $message, $headers, $attachments );
    }
}

Like Shaun mentioned, you should always use an external email solution else your ip / domain will get blacklisted. You may even have email sending restrictions from your hosting provider.

Leave a Comment