• I want to filter comments in admin panel by user. It mean each user that login to admin panel can see his/her own comments of posts.
    I used plugins such as “View Own Posts Media Only” but it has bug in comments count. I also try to add code to function.php and I could filter comments but still I can’t fix comments count.
    I use this code:

    
    function wps_get_comment_list_by_user($clauses) {
        if (is_admin()) {
            global $user_ID, $wpdb;
            $clauses['join'] = ", ".$wpdb->base_prefix."posts";
            $clauses['where'] .= " AND ".$wpdb->base_prefix."posts.post_author = ".$user_ID." AND ".$wpdb->base_prefix."comments.comment_post_ID = ".$wpdb->base_prefix."posts.ID";
        };
        return $clauses;
    };
    if(!current_user_can('edit_others_posts')) {
        add_filter('comments_clauses', 'wps_get_comment_list_by_user');
    }
    

    Please help me, Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The counts at the top of the comments list table? All, Pending, Approved, etc.? Use the ‘comment_status_links’ filter, you can revise the counts for each item in the array. There’s no requirement for the counts to be realistic. But for real counts, you may need to do your own count query. WP gets counts with wp_count_comments(), but it’ll likely return the same wrong count you see now.

    Thread Starter karimi5555

    (@karimi5555)

    Thanks for your reply @bcworkz.
    I’m not expert in WordPress can you write the code to fix comments count?
    I found this code but I can’t change it to fix my problem

    
    
    function wpse56652_filter_comments_count() {
        global $post_id, $comment_status, $comment_type;
    
        $status_links = array();
        $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments();
    
        $stati = array(
            /* translators: %s: all comments count */
            'all' => _nx_noop(
                'All <span class="count">(%s)</span>',
                'All <span class="count">(%s)</span>',
                'comments'
            ), // singular not used
    
            /* translators: %s: pending comments count */
            'moderated' => _nx_noop(
                'Pending <span class="count">(%s)</span>',
                'Pending <span class="count">(%s)</span>',
                'comments'
            ),
    
            /* translators: %s: approved comments count */
            'approved' => _nx_noop(
                'Approved <span class="count">(%s)</span>',
                'Approved <span class="count">(%s)</span>',
                'comments'
            ),
    
            /* translators: %s: spam comments count */
            'spam' => _nx_noop(
                'Spam <span class="count">(%s)</span>',
                'Spam <span class="count">(%s)</span>',
                'comments'
            ),
    
            /* translators: %s: trashed comments count */
            'trash' => _nx_noop(
                'Trash <span class="count">(%s)</span>',
                'Trash <span class="count">(%s)</span>',
                'comments'
            )
        );
    
        if ( !EMPTY_TRASH_DAYS )
            unset($stati['trash']);
    
        $link = admin_url( 'edit-comments.php' );
        if ( !empty($comment_type) && 'all' != $comment_type )
            $link = add_query_arg( 'comment_type', $comment_type, $link );
    
        foreach ( $stati as $status => $label ) {
            $current_link_attributes = '';
    
            if ( $status === $comment_status ) {
                $current_link_attributes = ' class="current" aria-current="page"';
            }
    
            if ( !isset( $num_comments->$status ) )
                $num_comments->$status = 10;
            $link = add_query_arg( 'comment_status', $status, $link );
            if ( $post_id )
                $link = add_query_arg( 'p', absint( $post_id ), $link );
            /*
            // I toyed with this, but decided against it. Leaving it in here in case anyone thinks it is a good idea. ~ Mark
            if ( !empty( $_REQUEST['s'] ) )
                $link = add_query_arg( 's', esc_attr( wp_unslash( $_REQUEST['s'] ) ), $link );
            */
            $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . sprintf(
                    translate_nooped_plural( $label, $num_comments->$status ),
                    sprintf( '<span class="%s-count">%s</span>',
                        ( 'moderated' === $status ) ? 'pending' : $status,
                        number_format_i18n( $num_comments->$status )
                    )
                ) . '</a>';
        }
    
        /**
         * Filters the comment status links.
         *
         * @since 2.5.0
         *
         * @param array $status_links An array of fully-formed status links. Default 'All'.
         *                            Accepts 'All', 'Pending', 'Approved', 'Spam', and 'Trash'.
         */
        return $status_links;
    }
    add_filter('comment_status_links', 'wpse56652_filter_comments_count');
    
    • This reply was modified 6 years, 3 months ago by karimi5555.
    Moderator bcworkz

    (@bcworkz)

    There’s no need to redo the entire nav links array. Simply replace one of the established links with your own version. I can’t give you complete working code, but if you plug in a proper SQL count query to the following code, it should give you a proper count (untested).

    function wpse56652_filter_comments_count( $data ) {
       global $wpdb;
       $count = $wpdb->get_result('----Insert SQL count query here----');
       $url = admin_url();
       $data['approved'] = "<a href=\"{$url}edit-comments.php?comment_status=approved\" >For Me <span class=\"count\">($count)</span></a>";
       return $data
    }
    add_filter('comment_status_links', 'wpse56652_filter_comments_count');
    Thread Starter karimi5555

    (@karimi5555)

    Thanks @bcworkz.
    Finally I fixed comments count problem by following code:

    
    function filter_comments_count() {
        global $post_id, $user_ID, $comment_status, $comment_type;
    
        $status_links = array();
    //    $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments();
        if (is_admin() && !current_user_can('edit_others_posts')) {
            $num_comments = get_user_comment_count($post_id, $user_ID);
        }
        else {
            $num_comments = get_user_comment_count($post_id);
        }
        $num_comments['moderated'] = $num_comments['awaiting_moderation'];
        unset( $num_comments['awaiting_moderation'] );
        $num_comments = (object) $num_comments;
    
        $stati = array(
            /* translators: %s: all comments count */
            'all' => _nx_noop(
                'All <span class="count">(%s)</span>',
                'All <span class="count">(%s)</span>',
                'comments'
            ), // singular not used
    
            /* translators: %s: pending comments count */
            'moderated' => _nx_noop(
                'Pending <span class="count">(%s)</span>',
                'Pending <span class="count">(%s)</span>',
                'comments'
            ),
    
            /* translators: %s: approved comments count */
            'approved' => _nx_noop(
                'Approved <span class="count">(%s)</span>',
                'Approved <span class="count">(%s)</span>',
                'comments'
            ),
    
            /* translators: %s: spam comments count */
            'spam' => _nx_noop(
                'Spam <span class="count">(%s)</span>',
                'Spam <span class="count">(%s)</span>',
                'comments'
            ),
    
            /* translators: %s: trashed comments count */
            'trash' => _nx_noop(
                'Trash <span class="count">(%s)</span>',
                'Trash <span class="count">(%s)</span>',
                'comments'
            )
        );
    
        if ( !EMPTY_TRASH_DAYS )
            unset($stati['trash']);
    
        $link = admin_url( 'edit-comments.php' );
        if ( !empty($comment_type) && 'all' != $comment_type )
            $link = add_query_arg( 'comment_type', $comment_type, $link );
    
        foreach ( $stati as $status => $label ) {
            $current_link_attributes = '';
    
            if ( $status === $comment_status ) {
                $current_link_attributes = ' class="current" aria-current="page"';
            }
    
            if ( !isset( $num_comments->$status ) )
                $num_comments->$status = 10;
            $link = add_query_arg( 'comment_status', $status, $link );
            if ( $post_id )
                $link = add_query_arg( 'p', absint( $post_id ), $link );
            /*
            // I toyed with this, but decided against it. Leaving it in here in case anyone thinks it is a good idea. ~ Mark
            if ( !empty( $_REQUEST['s'] ) )
                $link = add_query_arg( 's', esc_attr( wp_unslash( $_REQUEST['s'] ) ), $link );
            */
            $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . sprintf(
                    translate_nooped_plural( $label, $num_comments->$status ),
                    sprintf( '<span class="%s-count">%s</span>',
                        ( 'moderated' === $status ) ? 'pending' : $status,
                        number_format_i18n( $num_comments->$status )
                    )
                ) . '</a>';
        }
    
        /**
         * Filters the comment status links.
         *
         * @since 2.5.0
         *
         * @param array $status_links An array of fully-formed status links. Default 'All'.
         *                            Accepts 'All', 'Pending', 'Approved', 'Spam', and 'Trash'.
         */
        return $status_links;
    }
    function get_user_comment_count( $post_id = 0, $user_id = 0) {
        global $wpdb;
    
        $post_id = (int) $post_id;
        $user_id = (int) $user_id;
    
        $where = 'WHERE p.ID = c.comment_post_ID';
        if ( $post_id > 0 ) {
            $where .= $wpdb->prepare(" AND c.comment_post_ID = %d", $post_id);
        }
    
        if ($user_id > 0) {
            $where .= $wpdb->prepare(" AND p.post_author = %d", $user_id);
        }
    
        $totals = (array) $wpdb->get_results("
    		SELECT comment_approved, COUNT( * ) AS total
    		FROM {$wpdb->comments} c, {$wpdb->posts} p
    		{$where}
    		GROUP BY comment_approved
    	", ARRAY_A);
    
        $comment_count = array(
            'approved'            => 0,
            'awaiting_moderation' => 0,
            'spam'                => 0,
            'trash'               => 0,
            'post-trashed'        => 0,
            'total_comments'      => 0,
            'all'                 => 0,
        );
    
        foreach ( $totals as $row ) {
            switch ( $row['comment_approved'] ) {
                case 'trash':
                    $comment_count['trash'] = $row['total'];
                    break;
                case 'post-trashed':
                    $comment_count['post-trashed'] = $row['total'];
                    break;
                case 'spam':
                    $comment_count['spam'] = $row['total'];
                    $comment_count['total_comments'] += $row['total'];
                    break;
                case '1':
                    $comment_count['approved'] = $row['total'];
                    $comment_count['total_comments'] += $row['total'];
                    $comment_count['all'] += $row['total'];
                    break;
                case '0':
                    $comment_count['awaiting_moderation'] = $row['total'];
                    $comment_count['total_comments'] += $row['total'];
                    $comment_count['all'] += $row['total'];
                    break;
                default:
                    break;
            }
        }
    
        return $comment_count;
    }
    add_filter('comment_status_links', 'filter_comments_count');
    
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show own comments in admin panel’ is closed to new replies.