我正在尝试更新post meta值,但它不起作用。唯一有效的功能是get\\u post\\u meta和add\\u post\\u meta,但更新根本不起作用。此外,我还尝试使用wpdb->update更改值,但这也行不通。
这是代码。
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();
}
}
最合适的回答,由SO网友:HU ist Sebastian 整理而成
好您可以在$\\u POST中使用正确的字段,即$\\u POST[\'POST\\u id\'],而不是$\\u POST[\'POST-id\'];)
代替
update_post_meta($_POST[\'post-id\'], \'whole_avg\',$whole_avg);
使用
update_post_meta($_POST[\'post_id\'], \'whole_avg\',$whole_avg);
等等。
;)快乐的编码,
Kuchenundkakao