0

I have created this hook to send notification only when new post is published. In notification I am sending link and title of post which is newly created. For that, I need current post id inside function. How can I get it ?

add_action( 'new_to_publish', 'send_notification', 1 );
function send_notification(){

        $post_data['ID'] = the_ID();

        $post_id =  $post_data['ID'];
        global $wpdb;

        $sel2 = mysql_query("select * from gcm_users");
        while($data1  = mysql_fetch_assoc($sel2)) 
        {

            $key = $data1['gcm_regid'];
            include_once '../blogdroid/config.php';
            include_once '../blogdroid/GCM.php';

            $gcm = new GCM();
            $sel = mysql_query("select * from ".$wpdb->prefix."posts where ID='$post_id'");
            $data = mysql_fetch_assoc($sel);
            $Pname = $data['post_title'];
            $registatoin_ids = array($key);
            if($Pname != 'Auto Draft')
            {
                    $message = array("blog" => 'New Blog Arrived '.$Pname );

                    $result = $gcm->send_notification($registatoin_ids,$message);
            }

        }
  }
}
1

The new_to_publish hook has one argument: $post. Use that to get the post id. Here is your editted code:

add_action( 'new_to_publish', 'send_notification', 1, 1 );
function send_notification( $post ){    
        $post_id =  $post->ID;;
        global $wpdb;

        $sel2 = mysql_query("select * from gcm_users");
        while($data1  = mysql_fetch_assoc($sel2)) 
        {

            $key = $data1['gcm_regid'];
            include_once '../blogdroid/config.php';
            include_once '../blogdroid/GCM.php';

            $gcm = new GCM();
            $sel = mysql_query("select * from ".$wpdb->prefix."posts where ID='$post_id'");
            $data = mysql_fetch_assoc($sel);
            $Pname = $data['post_title'];
            $registatoin_ids = array($key);
            if($Pname != 'Auto Draft')
            {
                    $message = array("blog" => 'New Blog Arrived '.$Pname );

                    $result = $gcm->send_notification($registatoin_ids,$message);
            }

        }
  }
}
|improve this answer|||||
  • Actually i needed draft_to_publish, to run this function only on new post. – user3288891 May 17 '15 at 7:00
  • that should work the same way. – Manny Fleurmond May 17 '15 at 12:57

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.