使用更新用户元对用户进行排名

时间:2014-07-21 作者:andy

我正在使用一个插件,允许用户对帖子进行投票,我想将每个投票添加到authors user\\u meta,这样我就可以像对帖子进行排名一样对用户进行排名。

插件中有几个不同的函数可以更新和保存post\\u meta。

add_action( \'wp_ajax_nopriv_epicred_vote\', \'epicred_vote\' );
add_action( \'wp_ajax_epicred_vote\', \'epicred_vote\' );
    function epicred_vote(){
        global $wpdb, $current_user;

        get_currentuserinfo();

        $wpdb->myo_ip   = $wpdb->prefix . \'epicred\';

        $option = (int)$_POST[\'option\'];
        $current = (int)$_POST[\'current\'];

        //if we are locked via IP set the fid variable to be the IP address, otherwise log the member ID
        if(get_option(\'epicred_ip\') == \'yes\'){
            $ipAddr = isset($_SERVER[\'HTTP_X_CLUSTER_CLIENT_IP\']) ? $_SERVER[\'HTTP_X_CLUSTER_CLIENT_IP\'] : $_SERVER[\'REMOTE_ADDR\'];
            $fid = "\'" . $ipAddr . "\'"; 
        }else{
            $fid = $current_user->ID;
        }


        $postid = (int)$_POST[\'poll\'];  

        $query = "SELECT epicred_option FROM $wpdb->myo_ip WHERE epicred_ip = $fid AND epicred_id = $postid";

        $al = $wpdb->get_var($query);


        if($al == NULL){
            $query = "INSERT INTO $wpdb->myo_ip ( epicred_id , epicred_ip, epicred_option) VALUES ( $postid, $fid, $option)";
            $wpdb->query($query);
        }else{
            $query = "UPDATE $wpdb->myo_ip SET epicred_option = $option WHERE epicred_ip = $fid AND epicred_id = $postid";
            $wpdb->query($query);
        }

        $vote = get_post_meta($postid,\'epicredvote\',true);

            if($option == 1){
                if($al != 1){
                    if($al == -1){
                    $vote = $vote+2;    
                    }else{
                    $vote = $vote+1;
                    }
                }
            }


            if($option == -1){

                if($al != -1){
                    if($al == 1){
                        $vote = $vote-2;
                    }else{
                    $vote = $vote-1;
                    }   
                }   
            }
            update_post_meta($postid,\'epicredvote\',$vote);


            $response[\'poll\'] = $postid;
            $response[\'vote\'] = $vote;

        echo json_encode($response);

        // IMPORTANT: don\'t forget to "exit"
        exit;
    }
以及

function wpt_save_wpeddit() {

     global $wp, $wpdb, $post;  


    // unhook this function so it doesn\'t loop infinitely
    remove_action(\'save_post\', \'wpt_save_wpeddit\');


    // Is the user allowed to edit the post or page?
    if ( !current_user_can( \'edit_post\', $post->ID ))
    return $post->ID;
    // OK, we\'re authenticated: we need to find and save the data. Make sure we don\'t add an image every time the post is
    // saved as a draft - so keep track of the external URL in a custom field.


        $vote = get_post_meta($post->ID,\'epicredvote\',true);
        $rank = get_post_meta($post->ID,\'epicredrank\',true);

        if($vote == ""){
            $vote = 0;
        }
        if($rank == ""){
            $rank = 0;
        }

        update_post_meta($post->ID, \'epicredvote\', $vote );
        update_post_meta($post->ID,\'epicredrank\',$rank);


    add_action(\'save_post\', \'wpt_save_wpeddit\');

}
然后,我的目标是使用类似的方法对用户进行排名:-

编辑:-我用于输出用户/分数的循环。

 <?php


                        $args = array(
                            \'meta_key\' => \'epicredvote\',
                            \'orderby\'  => \'meta_value\',
                            \'order\'    => \'DESC\',
                            \'number\' => 10,

                        );

                        add_action( \'pre_user_query\', \'wpse_149342_pre_user_query\' );

                        $user_query = new WP_User_Query(  $args );

                        // User Loop
                        if ( ! empty( $user_query->results ) ) {
                            foreach ( $user_query->results as $user ) {
                                echo \'<p>\' . $user->display_name . \'</p>\';
                               echo \'<span>\' . $user->epicredvote . \'</span>\'; 
                            }
                        } else {
                            echo \'No users found.\';
                        }
                    ?>
因此,对一个帖子投一票就等于对该用户投一票。

可能,还是我完全偏离了轨道?

1 个回复
最合适的回答,由SO网友:karpstrucking 整理而成

看起来插件正在使用update_post_meta 函数,该函数反过来使用update_metadata 包含动作挂钩的函数update_postmetaupdated_postmeta 在post元数据存储到数据库之前和之后立即触发。您可以挂接其中一个来更新文章作者的用户元。伪代码:

add_action( \'update_postmeta\', \'wpse155265_update_user_vote\', 10, 4 );
function wpse155265_update_user_vote( $meta_id, $object_id, $meta_key, $meta_value ) {
  $post = get_post( $object_id );
  $user_id = $post->post_author;
  $votes = (int) get_user_meta( $user_id, \'epicredvote\', true );
  $votes++;
  update_user_meta( $user_id, \'epicredvote\', $votes );
}
编辑:

如上所述,上面没有考虑后元更新是赞成票还是反对票。以下修订代码;我们将比较post meta的值,新旧的,以确定用户的meta应该增加还是减少。

add_action( \'update_postmeta\', \'wpse155265_update_user_vote\', 10, 4 );
function wpse155265_update_user_vote( $meta_id, $object_id, $meta_key, $meta_value ) {
  $post = get_post( $object_id );
  $user_id = $post->post_author;
  $meta_value_old = get_post_meta( $object_id, \'epicredvote\', true );
  $votes = (int) get_user_meta( $user_id, \'epicredvote\', true );
  ( $meta_value > $meta_value_old ) ? $votes++ : $votes--;
  update_user_meta( $user_id, \'epicredvote\', $votes );
}
我刚才想到的另一种方法是,在对用户进行排名时,只计算用户的投票,而不是将他们的投票存储在meta中。循环遍历来自的所有用户get_users 对于每个用户,计算他们的总投票数,将每个用户存储到一个数组中,以他们的总投票数为关键字,使用ksort 然后在数组中循环以输出您的排名列表。不过,为每个用户运行WP\\u查询可能有点紧张。也许有一条SQL语句可以组合在一起,它将实际返回按投票总数排序的所有用户,但我不得不修改它,看看这是否可行。

编辑:

这里有一个SQL,可以计算用户并对其进行排名。将此插入$wpdb 根据需要:

global $wpdb;
$results = $wpdb->get_results(
  "
    SELECT u.display_name as name,
    ( SELECT SUM(pm.meta_value)
      FROM wp_posts p, wp_postmeta pm
      WHERE p.post_author = u.ID
      AND p.post_status LIKE \'publish\'
      AND pm.meta_key = \'epicredvote\'
      AND p.ID = pm.post_id ) as votes
    FROM wp_users u
    ORDER BY votes DESC
  "
);
foreach ( $results as $result ) {
  echo "{$result->name}: {$result->votes} votes<br>";
}

结束

相关推荐

Loop get_theme_mod

我已经设置了一系列自定义设置与其为每个设置单独调用单独的get\\u theme\\u mod语句,是否有一种方法可以简化它,使其循环通过它们,而不必显式地单独编写每个设置?完整文件也在上设置Gist 供审查<?php /** Adds the Customize page to the WordPress admin area */ function litho_customizer_menu() { add_theme_page( \'Cust