Skip Navigation

Notifications when custom field value changes not sent if updated programmatically

Won't fix

Symptoms

Form notifications can be triggered when a custom field value changes, if it matches the target value.

Such notifications are sent if the post is edited by a front-end form, or from the back-end post edit screen.

The notifications are not sent if the custom field value is changed programmatically, i.e. using the update_post_meta function.

Workaround

This is by design. Checking if a custom field value has changed (and matches) every single time any custom field value on the site it updated is inefficient and could lead to performance issues.

A workaround when updating custom field values programmatically is not to update the post meta directly (using update_post_meta), but indirectly by updating the post itself (using wp_update_post).

wp_update_post accepts an argument ‘meta_input’ that is used to update post meta belonging to the post, which is an array of postmeta keys and values. (https://developer.wordpress.org/reference/functions/wp_update_post/)

So instead of this:


update_post_meta( $post->ID, 'wpcf-choice', 3 );

you could use this, which would trigger the notification:


wp_update_post( array( 'ID' => $post->ID, 'meta_input' => array( 'wpcf-choice' => 3 ) ) );

Leave
a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>