Notify Author of the post if admin deletes his post and perform some function
Question
i am working on a theme in which points are added in user meta when he does a post... it works fine but the problem is when a post is deleted by the admin then the point should be reduced and the author should get intimation . The main problem is getting the author id of the post and doing function after the post is deleted....
the code i am trying is
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');
Solution
Firstly, it's better to user WP functions than write your own SQL. Even though it might be a bit more expensive:
$post = get_post($post_ID);
$authorid = $post->post_author;
Secondly, it's not get_usermeta()
, but get_user_meta()
and the complete call should include true
as the third parameter:
$currentPointNumber = get_user_meta($authorid, 'points', true);
And then it's update_user_meta()
, again, and you don't need to initialize a new variable, just do:
update_user_meta($authorid, 'points', $currentPointNumber-1);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange