在钩子内获取当前帖子ID

时间:2015-05-15 作者:Suraj

我创建这个钩子是为了仅在发布新帖子时发送通知。在通知中,我正在发送新创建的帖子的链接和标题。为此,我需要函数中当前的post id。我怎样才能得到它?

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 个回复
最合适的回答,由SO网友:Manny Fleurmond 整理而成

这个new_to_publish 钩子有一个参数:$post. 使用它来获取帖子id。这是您编辑的代码:

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);
            }

        }
  }
}

结束

相关推荐

numberposts not working

我有以下代码块,希望将帖子数量限制为2篇,但似乎不起作用。。??我也尝试过在每页上使用posts\\u,但似乎也不起作用。有谁能帮我指出哪里出了问题吗?<?php $tax_country = strip_tags( get_the_term_list($post->ID, \'country\') ); echo \'<h1>More Properties in \'; echo $tax_country; echo \'...