WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So far this code works if I run it as a page in WordPress. I am trying to send my user email updates when I publish a post with with a specific tag. The users can choose if they want to receive the updates in their settings page.

  1. Every time a post is published it checks if the post has the specific tag. Only one tag will be added to the post.
  2. then is checks if the tag match the users settings (first user setting)
  3. then is check if the users have updates turned on. (Second user setting)
  4. if everything is good it should send the email.

But it does not send the email if the code is in the function file. I do not get an error, so I'm kinda stuck.

// check is new post is published
 add_action( 'transition_post_status', 'a_new_post', 10, 3 );
function a_new_post( $new_status, $old_status, $post ) { 
 if ( 'publish' !== $new_status or 'publish' === $old_status ) { 
 global $post;  global $wp_query;
 // get all the users meta
  $blogusers = get_users(); 

   // get users facility updates setting from settings page
 $arg = array(
  'posts_per_page' => '1'
  // start the loop the get only the last post
 $wp_query = new WP_Query($arg);
  query_posts( 'posts_per_page=1 ');
   if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
  // get the post tags
$posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { 

 foreach ($blogusers as $user) { 
  // get the users settings and check if they match
  $savedsetting = $user->facilityupdates; 
  $current_facility = $user->current_facility;

  if($savedsetting === 'ON' && $tag->name == $current_facility )  {
 // get the users emails
    $users_emails = $user->user_email . ',';

   // send email

  $to = $users_emails; 
             $subject = "New update from PrisonPulse";
              $message = "a caller who phone number is was sent to  wanting to go to  (second request) ";
               $from = "example@gmail.com"; 
               $headers = "From:" . $from; 

  }}} }
    endwhile; endif; // end the loop
share|improve this question
up vote 1 down vote accepted

The post's data is passed as an argument to the function hooked to transition_post_status, so you don't need to query for anything (also, your query is a bit strange, using both WP_Query and query_posts, but that's a separate matter). Here's a trimmed version of your function showing the important changes:

function a_new_post( $new_status, $old_status, $post ){
    if ( 'publish' !== $new_status or 'publish' === $old_status )

    $posttags = get_the_terms( $post->ID, 'post_tag' );
    if( $posttags ) {
        $blogusers = get_users();
        foreach( $posttags as $tag ) {
            // the rest of your code
share|improve this answer
Thank you @Milo. Your version worked great. Helped me to fix a few more issues I was having also. – J. F. Bond Feb 11 '13 at 14:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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