代码之家  ›  专栏  ›  技术社区  ›  Gary Woods

WordPress:将用户的评论数添加到他们的用户元数据中

  •  1
  • Gary Woods  · 技术社区  · 10 年前

    我使用以下代码获取 用户对其他用户帖子的评论总数 (不包括在用户自己的帖子中发表的评论):

    function grab_custom_user_comments_count( $uid ){
        global $wpdb;
        $sql = "SELECT COUNT(*) as total 
                FROM {$wpdb->comments} as c  
                JOIN {$wpdb->posts} as p ON p.ID = c.comment_post_ID 
                WHERE c.comment_approved = '1' 
                AND p.post_status ='publish'  
                AND p.post_type ='post'  
                AND p.post_author != c.user_id            
                AND c.user_id = %d";
    
        $comment_count_others = $wpdb->get_var( $wpdb->prepare( $sql, $uid ) );
        return $comment_count_others;
    }
    $user_comment_count = grab_custom_user_comments_count( $userID );
    

    现在,我想更新所有用户的 metadata 具有此值。所以 $meta_value 将是评论总数(如上所示)和 $meta_key 可以是 custom_user_comments .

    如何为所有用户添加此元数据? 我如何使其在用户对其他用户的帖子发表评论时更新值?

    1 回复  |  直到 10 年前
        1
  •  2
  •   brasofilo Gary    10 年前

    使用插件:

    • 在插件激活时修改所有用户元
    • 检查每个发布的评论,必要时进行更新

    您必须使用每个函数中提供的用户ID来填补空白:

    <?php
    /**
     * Plugin Name: (SO) Comment to User Meta
     * Plugin URI: http://stackoverflow.com/a/25464099/1287812
     * Author: brasofilo
     */
    
    /**
     * Modify all user meta on plugin activation
     */
    register_activation_hook(   __FILE__, function() 
    {
        # Security checks
        if ( ! current_user_can( 'activate_plugins' ) )
            return;
    
        $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
        check_admin_referer( "activate-plugin_{$plugin}" );
    
        # ITERATE THROUGH USERS AND UPDATE METADATA
        foreach( get_users() as $user ) 
        {
            # http://codex.wordpress.org/Function_Reference/update_user_meta
            #
            $user_comment_count = grab_custom_user_comments_count( $user->data->ID );
        }
    });
    
    /**
     * Update user meta on comment post
     * looks like this hook only runs for logged users, checking for that just to be safe
     *
     * @param $id           Comment ID
     * @param $comment_obj  Full comment details
     */
    add_action( 'wp_insert_comment', function( $id, $comment_obj ) 
    {
        # Only on backend and for logged users
        if( is_admin() || !is_user_logged_in() )
            return; 
    
        # CURRENT LOGGED USER
        global $user_ID;
        get_currentuserinfo();
    
        # POST AUTHOR
        # http://codex.wordpress.org/Function_Reference/update_user_meta
        #
        $user_comment_count = grab_custom_user_comments_count( $comment_obj->user_id );
        // You can compare $user_ID and $comment_obj->user_id here
    }, 10, 2 );
    

    这个 $comment_obj 已通过 wp_insert_comment hook包含如下数组,您可以使用 get_post( $comment_obj->comment_post_ID ) 获取有关当前帖子的信息。

    stdClass Object
    (
        [comment_ID] => 36
        [comment_post_ID] => 885
        [comment_author] => nicename
        [comment_author_email] => email@mail.com
        [comment_author_url] => 
        [comment_author_IP] => 127.0.0.1
        [comment_date] => 2014-08-23 19:16:49
        [comment_date_gmt] => 2014-08-23 17:16:49
        [comment_content] => Lorem ipsum lorem
        [comment_karma] => 0
        [comment_approved] => 1
        [comment_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36 FirePHP/4Chrome
        [comment_type] => 
        [comment_parent] => 0
        [user_id] => 1
    )