0

I am trying to update post meta values but it does't work. Only function that is working is get_post_meta and add_post_meta but Update wont work at all. Also I tried to change the values using wpdb->update but that wont work either.

Here is the code.

function rating_system_callback(){

if(  !wp_verify_nonce(  $_POST['nonce'] ,'star_rating_nonce' )){
    wp_die('No nonce found.');
}
if(record_check_and_add($_POST)){
    $total_user_voted = get_post_meta( $_POST['post_id'], 'no_of_user_voted', true );


    if(empty($total_user_voted)){
        add_post_meta($_POST['post_id'], 'no_of_user_voted', 1, true);
    }
    $new_user = $total_user_voted + 1;
    update_post_meta($_POST['post-id'], 'no_of_user_voted', '58');


    if(empty($total_votes_for_post)){
        add_post_meta($_POST['post_id'], 'total_points_for_post', $_POST['clicked_on'], true);

    }
    $new_points =  $total_votes_for_post + $_POST['clicked_on'];
    update_post_meta($_POST['post-id'], 'total_points_for_post',$new_points);

    $whole_avg = round($new_points / $new_user, 1) ;

    $avg = get_post_meta($_POST['post_id'], 'whole_avg', $whole_avg, true);
    if(empty($avg)){
        add_post_meta($_POST['post_id'], 'whole_avg', $whole_avg, true);

    }
    update_post_meta($_POST['post-id'], 'whole_avg',$whole_avg);
    wp_die();
}

}

0

Well.... you could use the correct Field in the $_POST, which would be $_POST['post_id'], not $_POST['post-id'] ;)

Replace

update_post_meta($_POST['post-id'], 'whole_avg',$whole_avg);

with

update_post_meta($_POST['post_id'], 'whole_avg',$whole_avg);

etc.

;) Happy Coding,

Kuchenundkakao

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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