I am trying to create a multi criteria post rating function with a simple form and post_meta and user meta.
The Goal
-------------
when users submits their ratings 2 things should happen:
[login to view URL] 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
)
)
[login to view URL] 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 [login to view URL]:**
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
1. 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.
2. The code is not checking for the existing user rating and replacing the old ratings with the new one.
p.s: you can add this to any [login to view URL] file and check for the post_meta database to check.
Hello,
We can create the fix a custom wordpress function that I wrote based on your requirements and timeline.
I primarily work with small businesses, professional services and individuals. My goal is to provide quality web solution at a reasonable price.
Please let me know your availability and we can discuss the details over Skype or Phone. We have local office in US as well.
Regards,
Ash
Hello,
We have vast Experience on Website Developing as we are in the market from last 3+ years. We have Sufficient and Well trained Employee Team including Website Developers, Web Designers, SEO Team.
We have read your all requirement and would like to have some more words on it.
As security purpose on this website we can not see your Website URL, so also we have to discuss on it.
We can complete your task within Timeframe.
All Portfolios will be provided on request.
Waiting for your Favorable Response.
Thanks
Hi there
Got your code ready, interesting problem (allthough there are easier ways to fix it). The solution was to pull out the arrays from wpd_rating before the post_meta array_merge.
Add me to the project, and I'll send you the code.
Thanks,
Alex