Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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');
    
    
    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.
Viewing 2 replies - 1 through 2 (of 2 total)