I want to send email if a postmeta of a post is changed to the expected value.So previously i was doing loop to check the post meta in each post. In here am running the loop in admin_init hook. But this slow down the site, so i'm thinking to do this on save post only. SO the edited post may have the meta value am expecting then i'll send the mail.


$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) {
        //mail() function goes here



This is unwanted because only certain post may be edited. Blindly checking post meta with a loop every time is waste.

So i thought to use on save_post on save we'll check whether the post meta is changed to the expected value then we'll do the mail.like below

function testing($post_id){
        //mail() function goes here
        //only for edited post alone i'm checking here


There are another two scenario where the post meta may be changed which are bulk edit and quick edit

For quick edit i found this

function check(){
var_dump($_REQUEST);//can able to get the post id, i can do the rest

But for bulk edit i don't have any clue. I tried searching, In this answer i found that $_REQUEST may contain action and postid. So i thought for bulk edit there might be an action in $_REQUEST['action'] but i couldn't find when i try like this

function another_check(){


I found this check_admin_referer too but i couldn't able to figure out as it does not print when i var dump. Will this(check_admin_referer) work or suit on this? Is there any other easy way to get the edited post ids?

How can i get the List of post id on bulk edit? From then i can able to do the check and send mail accordingly

1 Answer 1


If your problem is:

I want to send email if a postmeta of a post is changed to the expected value

Why do yous ask for:

How can i get the List of post id on bulk edit?

This is a typical x/y problem: when you have a problem, ask how to solve that problem, instead of asking ways to apply what you think is the solution...

Going in the details, you want to perform an action when a meta field is updated? Why don't just look at.. the moment it is updated?

Everytime a post meta is updated, WordPress call the hook 'updated_postmeta' like so

do_action("updated_{$type}_meta", $meta_id, $object_id, $meta_key, $meta_value);

where $type is 'post'.

As you can see, you have enough informations to do anything you want.

Let's assume you want to send a mail to the post author everytime the meta 'send_mail' is set to 'ok':

add_action( 'updated_post_meta', 'listen_to_meta_change', 20, 4 );

function listen_to_meta_change( $mid, $pid, $key, $value ) {
  if ( $key !== 'send_mail' ) return; // if the key is not the right one do nothing
  $value = maybe_unserialize( $value );
  if ( $value !== 'on' ) return; // if the value is not the right one do nothing
  // if we're here, the post meta 'send_mail' was set to 'on' for a post, let's get it
  $post = get_post( $pid );
  if ( $post->post_type !== 'post' ) return; // maybe check for a specific post type?
  $recipient = new WP_User( $post->post_author ); // get the post author
  if ( ! $recipient->exists() ) return; // check if is a valid user

  static $sended = array();
  if ( ! isset($sended[$recipient->ID]) ) $sended[$recipient->ID] = array();
  if ( isset($sended[$recipient->ID][$pid]) ) {
    // already sent email for this user & post & request: that's enough, let's exit
  $sended[$recipient->ID][] = $pid;

  // ok, send the email, you can write the function, by yourself, isn't it?
  // be sure to have control on how many emails you send to an user:
  // too much emails slow down your site and also make you a spammer...
  // probably you can take control that using an user meta...
  send_email_to_user_when_meta_updated( $recipient );

Note that this code runs only when a meta is updated, and not when is added.

To run same code when the meta is also added, just add another action:

add_action( 'added_post_meta', 'listen_to_meta_change', 20, 4 );

Both hooks work in identical way, passing identical arguments, so no problem on using same function for both.

  • Thanks.Looks you are targeting exactly. But it is not working on bulk edit and quick edit. Why am i talking about bulk and quick edit because on bulk and quick edit this post meta may change, at this point also we need to check and perform action. But the function am hooking is not happening on bulk,quick edit. It works only on update the product from single product page. Am trying this in woocommerce. Does it makes any sense? @G.M.
    – sun
    Apr 7, 2014 at 7:06
  • @sun How do you edit post meta on bulk edit?
    – gmazzap
    Apr 7, 2014 at 8:28
  • Select the products then edit, In there changing the Visibility or Tax Status in select boxes and then hit update
    – sun
    Apr 7, 2014 at 8:42
  • @sun visibility and taxomomy are not post meta
    – gmazzap
    Apr 7, 2014 at 9:18
  • I just showed them for example purpose. lets take price or stock for example. stock('_stock_status') is a post meta, i think. Check this link Where its check whether post meta(_stock_status) is updated or not. Let me know if this(_stock_status) is not post meta.
    – sun
    Apr 7, 2014 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.