根据documentation, transition_post_status 操作仅接收3个参数($old_status, $new_status, 和$post).
没有$category_id 参数,但由于post对象对您可用,您可以使用此post对象获取post类别。
就你而言if 声明,最好使用has_category() 可以传递类别ID和post对象的函数。
has\\u category()
检查当前帖子是否有任何给定类别。
因此,筛选代码应如下所示:
function telegram_send_message( $new_status, $old_status, $post) {
//$category_id==\'1766\' changed to has_category(1766, $post)
if( $new_status == \'publish\' && $old_status != \'publish\' && $post->post_type == \'post\'&& has_category(1766, $post) {
$apiToken = "TOKEN";
$data = [
\'chat_id\' => \'@******\',
\'text\' => "\\nRead more: " . get_permalink($post->ID)
];
$response = file_get_contents("https://api.telegram.org/bot******/sendMessage?" . http_build_query($data) );
}
}
add_action( \'transition_post_status\', \'telegram_send_message\', 10, 3 );