带有POST_META和USER_META的WordPress简单帖子多评级

时间:2014-02-04 作者:Towfiq

我正在尝试创建一个具有简单表单、post\\u meta和user meta的多标准后评价函数。

目标

当用户提交评分时,应该发生两件事:

1.评级存储在名为“wpd\\U评级”的post\\U meta中,如下所示:

array(user id => array(
          field1 => 5
          field2 => 3
          field3 => 4
          ),
      another user id => array(
          field1 => 2
          field2 => 5
          field3 => 4
          )

) 
2。评级存储在名为“plgn\\U rating”的user\\u meta中,如下所示:

array(post id => array(
          field1 => 5
          field2 => 3
          field3 => 4
          ),
      another rated post id => array(
          field1 => 2
          field2 => 5
          field3 => 4
          )

) 
代码(明显不起作用)

In single.php:

标题部分:

<?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);

    }

?>
表格:

<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

<数组未按我的结构保存。我很确定我使用的array\\u merge是错误的。或者调用get\\u post\\u meta错误。

代码没有检查现有的用户评级并用新的评级替换旧的评级。

正如s\\u ha\\u dum所建议的那样,更新并没有用:

foreach ( $ls_up_votes as $key => $value ){ 
var_dump($key,$value);

   if ( $key == $currentusr ){ 
      unset( $ls_up_votes[$key] );
   }
} 
当我添加用户输入2次时,unset命令应该删除同一用户添加的前一个数组并添加新数组,而不是全部添加2次。以下是我得到的:

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 ) ) 

1 个回复
SO网友:s_ha_dum

基于此:

Array ( 
  [0] => Array ( 
    [wpd_function_rating] => 33 
    [wpd_feature_rating] => 12 
    [wpd_support_rating] => 66 
  ) 
)
我至少能发现一个问题。您有一个嵌套数组,但仅在顶层循环。此处:

foreach ( $ls_up_votes as $key => $value )
      {
        if ( $key == $currentusr )
          {
            unset( $ls_up_votes[$key] );
      }
 }
试试看var_dump($key,$value) 在里面foreach.

我认为您的解决方案是将第三个参数传递给get_post_meta 因此它返回“single”,而不是数组。这有点奇怪,但与我认为的序列化数组有点反直觉。

$ls_up_votes = get_post_meta($postid, \'wpd_rating\');

结束