如果管理员删除他的帖子并执行一些功能,请通知该帖子的作者
题
我正在研究一个主题,在他执行帖子时,他在用户元中添加了点...工作正常,但是问题是当管理员删除帖子时,应减少该点,作者应获得暗示。主要问题是在删除帖子后获得帖子的作者ID和执行功能。
我正在尝试的代码是
function deletePointFromUser($post_ID) {
global $wpdb;
$authorid = $wpdb->get_var('SELECT post_author FROM wp_posts WHERE post_id = $post_ID');
$currentPointNumber = get_usermeta($authorid, 'points');
//Delete 1 to the current Point Score
$newPointNumber = $currentPointNumber - 1;
update_usermeta( $authorid, 'points', $newPointNumber);
}
add_action('deleted_post', 'deletePointFromUser');
解决方案
首先,用户WP功能比编写自己的SQL更好。即使可能更昂贵:
$post = get_post($post_ID);
$authorid = $post->post_author;
其次,不是 get_usermeta()
, , 但 get_user_meta()
完整的电话应包括 true
作为第三个参数:
$currentPointNumber = get_user_meta($authorid, 'points', true);
然后是 update_user_meta()
, ,同样,您无需初始化一个新变量,只需做:
update_user_meta($authorid, 'points', $currentPointNumber-1);