Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question
    
this looks about right , what is the problem/question? –  Bainternet Feb 24 '11 at 10:35
    
The get_var call needs double quotes, else that post id is getting treated literally as $post_id and not the numeric value it contains, which is probably why you're not getting back the expected author id. –  t31os Feb 24 '11 at 11:54

1 Answer 1

up vote 0 down vote accepted

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);
share|improve this answer
    
Just a note, there is a get_usermeta function, but it was deprecated since WP 3.0 and was replaced by get_user_meta, so technically the existing code wasn't incorrect, simply calling a deprecated function. –  t31os Feb 24 '11 at 16:38
    
Thanks it works perfectly fine .... and get_usermeta() is a function , it was deprecated since wordpress 3.0 with get_user_meta() and same goes for update_usermeta. –  Harjeet Singh Feb 28 '11 at 10:33
    
Yepp, thanks...was a bit hasty with my lecturing, there. ;-) –  wyrfel Feb 28 '11 at 13:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.