1

I'm attempting to send an email notification when a scheduled post is published. I've tried quite a few different ways. I receive the email but the id (and other random details) are not published with it. Is there something that I'm fundamentally missing in this code? I tried both outside and inside and a few other ways. The dap post is a fictional custom post type that I was initially testing on and then thought that it may be custom post types.

So I used a regular post and nothing. I also disabled all plugins except for the one containing the code below. Still nothing worked. I can see the details if I publish immediately but not from when it's scheduled.

add_action( 'init', 'create_dap_post_types' );

function create_dap_post_types() {
    register_post_type( 'dap', 
        array(
            'labels' => array(
                'name' => __( 'Daps' ),
                'singular_name' => __( 'Dap' )
            ),
            'public' => true,
        )
    );
}



function status_changes( $new_status, $old_status, $post ) {
 global $post;
 global $wpdb;
 global $wp_query;

 if ($new_status == 'publish') {
    //$post = get_post($id);
        $id = get_the_ID();
    $post = get_post($id);
    $post_title = $post->post_title;
      // $author = get_userdata($post->post_author);
     // the rest of your function here
     $link = get_permalink($post->ID);
    $date = mysql2date('M j Y', $post->post_date);
    $category = get_the_category($post->ID);
    //$category = $category[0]->cat_name;

    $to = "//left blank to void out email";
    $subject = $post_title;
    $message = "<br><h1> schedulenotificte </h1></br> Let's begin: author " . //$author .
                " the link? " . $link;
    wp_mail( $to, $subjet, $message);
 }


}  
//add_action( 'transition_post_status', 'status_changes', 100, 3 );


add_action('publish_'. $_POST['post_type'], 'scheduledNotification');
add_action('future_to_publish_dap', 'scheduledNotification');
add_action('new_to_publish_dap', 'scheduledNotification');
add_action('draft_to_publish_dap', 'scheduledNotification');
add_action('pending_to_publish_dap', 'scheduledNotification');
add_action('future_to_publish_dap', 'scheduledNotification');
add_action('auto-draft_to_publish_dap', 'scheduledNotification');
add_action('pending_to_future', 'scheduledNotification');
add_action('approved_to_future', 'scheduledNotification');

function scheduledNotification() {
  global $post;
  global $wp_query;
  $post = $wp_query->post;
  //$post = get_post($id);
  $my_postid = $post->ID;//This is page id or post id
  $content_post = get_post($my_postid);
  $content = $content_post->post_content;
  $content = apply_filters('the_content', $content);
  $content = str_replace(']]>', ']]&gt;', $content);

  $id = get_the_ID();
  $post = get_post($id);

  $link = get_permalink($post->ID);
  $date = mysql2date('M j Y', $post->post_date);
  $category = get_the_category($post->ID);


  $to = "//left blank";
  $subject = $post->post_title;
  $message = "<br><h1> schedulenot </h1></br> <h2><br>Let's begin shall we?:<h2></br>author " . //$author .
            " the link? " . $link . 
            "<br> this is the content </br>" . $content .
            "<br> this is the idea</br>" . $post->ID;
  wp_mail( $to, $subject, $message);
}

function hereIsATest ($post) {
  global $post;
  global $wp_query;
  $post = $wp_query->post;

  $my_postid = $post->ID;//This is page id or post id
  $content_post = get_post($my_postid);
  $content = $content_post->post_content;
  $content = apply_filters('the_content', $content);
  $content = str_replace(']]>', ']]&gt;', $content);



  $subject = $post->title;
  $message = "<br> hereisatest </br> this is the id " . $post->ID . 
        "This is content " . $post->content .
        "<br> the title </br>" . $post->post_title .
        "what else there" . $subject . 
        "<br> id </br>" . get_the_ID() . 
        "<br> id_ </br>" . $post->ID .
        "<br> id </br>" . $post->content_post;
   wp_mail("//blank", $subject , $message);
}

add_action('publish_'. $_POST['post_type'], 'hereIsATest');


add_action( 'transition_post_status', 'inLoop', 100, 3 );
add_action ('wp_transition_post_status', 'inLoop');

function inLoop($new_status, $old_status, $post) {
  global $post;
  $thePostID = $post->ID;

  if ($new_status == 'publish') {
    $to = "blank";
    $subject = $thePostID;
    $message = "<h1> This is insidethe loop</h1>" . $thePostID .
            "<br> the title ////" . get_the_title() .
            "<br> get the title </br>" . get_the_title() . 
            "<br> old status is: </br> " . $old_status . 
            "<br> new status is: </br> " . $new_status . 
            "<br> dump post: </br>" . $rpost;

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

add_action( 'transition_post_status', 'outLoop', 100, 3 );
add_action('wp_transition_post_status', 'outLoop');

function outLoop($new_status, $old_status, $post) {
  // Works in single posts outside of the loop
  global $wp_query;

  if ($new_status == 'publish') {
    $thePostID = $wp_query->post->ID;
    //$rpost = print_r($post, true);
    $to = "email";
    $subject = $thePostID;
    $message = "<h1> This is outside the loop</h1>" . $thePostID . 
            "<br> get the title </br>" . get_the_title() . 
            "<br> old status is: </br> " . $old_status . 
            "<br> new status is: </br> " . $new_status . 
            "<br> dump post: </br>";

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

}
1

The problem is that you are using get_the_ID() outside the loop. Also, you are getting the global $post overriding the $post object passed to the function and then overriding it again with get_post() function. As you have already the $post object which status has changed, you need nothing of the get_the_ID, get_post or global $post. Just use the $post object you already have.

add_action( 'transition_post_status', 'status_changes', 100, 3 );
function status_changes( $new_status, $old_status, $post ) {

    if ($new_status == 'publish') {

        $post_title = $post->post_title;
        $author = get_userdata($post->post_author);
        $link = get_permalink($post->ID);
        $date = mysql2date('M j Y', $post->post_date);
        $category = get_the_category($post->ID);
        $category = $category[0]->cat_name;

        $to = "//left blank to void out email";
        $subject = $post_title;
        $message = "<br><h1> schedulenotificte </h1></br> Let's begin: author " . //$author .
            " the link? " . $link;
        wp_mail( $to, $subjet, $message);
     }


}
  • Thank you. That actually makes sense when you think about wordpress. – Cody Sep 8 '14 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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