I am trying to create a multi criteria post rating function with simple form and post_meta and user meta.
The Goal
when users submits their ratings 2 things should happen:
1.the ratings is stored in the post_meta called 'wpd_rating' like this:
array(user id => array(
field1 => 5
field2 => 3
field3 => 4
),
another user id => array(
field1 => 2
field2 => 5
field3 => 4
)
)
2.the ratings is stored in the user_meta called 'plgn_rating' like this:
array(post id => array(
field1 => 5
field2 => 3
field3 => 4
),
another rated post id => array(
field1 => 2
field2 => 5
field3 => 4
)
)
The Code (Not working Obviously)
In single.php:
the header part:
<?php // top of page
if ( isset( $_POST['drw_inventory'] ) && wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta') )
{ //if nonce check succeeds.
global $post;
$postid = $post->ID;
$data = $_POST['wpd_function_rating'];
$data2 = $_POST['wpd_feature_rating'];
$data3 = $_POST['wpd_support_rating'];
$currentusr = get_current_user_id();
//*******************The post meta field Part****************************
//Get the Existing user ratings of this post
$ls_up_votes = get_post_meta($postid, 'wpd_rating');
//if the current user already rated, unset the rating
foreach ( $ls_up_votes as $key => $value )
{
if ( $key == $currentusr )
{
unset( $ls_up_votes[$key] );
}
}
//Add post meta 'wpd_rating' with this structure:
//array(userid => array('wpd_function_rating' => '3','wpd_feature_rating' => '5','wpd_support_rating' => '4',))
$up_vote = array($currentusr => array('wpd_function_rating' => $data,'wpd_feature_rating' => $data2,'wpd_support_rating' => $data3));
//appending new user ratings after one another
$ls_up_voted = array_merge($ls_up_votes, $up_vote);
update_post_meta($postid,'wpd_rating',$ls_up_voted);
//*******************The user field Part****************************
//Get the Current user ratings of this post
$user_rated_posts = get_user_meta($currentusr, 'plgn_rating',true);
foreach ( $user_rated_posts as $key => $value )
{
if ( $key == $postid )
{
unset( $user_rated_posts[$key] );
}
}
//add a user field 'plgn_rating' with this structure
//(postid => array('wpd_function_rating' => '3','wpd_feature_rating' => '5','wpd_support_rating' => '4',))
$usr_vote = array($postid => array('wpd_function_rating' => $data,'wpd_feature_rating' => $data2,'wpd_support_rating' => $data3));
//appending new user ratings after one another
$ls_up_voted = array_merge($user_rated_posts, $usr_vote);
update_user_meta($currentusr,'plgn_rating',$ls_up_voted);
}
?>
The Form:
<form method="post" action="">
<?php wp_nonce_field('update_drw_postmeta','drw_inventory'); ?>
<label>This is label</label>
<input type='text' name='wpd_function_rating' value='' />
<input type='text' name='wpd_feature_rating' value='' />
<input type='text' name='wpd_support_rating' value='' />
<input type='submit' value='save' />
</form>
Issues with the current code
The array is not saving as I structured. I am pretty sure I am using the array_merge wrong. or the calling the get_post_meta wrong.
The code is not checking for the existing user rating and replacing the old ratings with the new one.
Update
as s_ha_dum suggested tried this with no avail:
foreach ( $ls_up_votes as $key => $value ){
var_dump($key,$value);
if ( $key == $currentusr ){
unset( $ls_up_votes[$key] );
}
}
When I add the user input 2 times, the unset commands should remove the previous array that was added by the same user and add the new one, instead its adding all 2 times.Here is what I am getting:
Array ( [0] => Array ( [0] => [1] => Array ( [wpd_function_rating] => 33 [wpd_feature_rating] => 23 [wpd_support_rating] => 66 ) ) [1] => Array ( [wpd_function_rating] => 44 [wpd_feature_rating] => 11 [wpd_support_rating] => 32 ) )