WordPress Snippets at WPcustoms

Show Pingbacks count in post table

If your site gets trackbacks and pingbacks you can show your posts count in the admin panel. A new post column is added and the pingback\trackback count is grabbed from the database.


/**
 * Snippet Name: Show Pingbacks count in post table
 * Snippet URL: http://www.wpcustoms.net/snippets/show-pingbacks-count-in-post-table/
 */
 function wpc_pingback_count($type = 'comments'){
    if($type == 'trackbacks'):
        $typeSql = 'comment_type = "trackback"';
        $oneText = 'One trackback';
        $moreText = '% trackbacks';
        $noneText = 'No trackbacks';
    elseif($type == 'pingbacks'):
        $typeSql = 'comment_type = "pingback"';
        $oneText = 'One pingback';
        $moreText = '% pingbacks';
        $noneText = 'No pingbacks';
    endif;
    global $wpdb;
    $result = $wpdb->get_var('
        SELECT
            COUNT(comment_ID)
        FROM
            '.$wpdb->comments.'
        WHERE
            '.$typeSql.' AND
            comment_approved="1" AND
            comment_post_ID= '.get_the_ID()
    );
    if($result == 0):
        echo str_replace('%', $result, $noneText);
    elseif($result == 1):
        echo str_replace('%', $result, $oneText);
    elseif($result > 1):
        echo str_replace('%', $result, $moreText);
    endif;
}

function posts_columns_counts($defaults){
    $defaults['wps_post_counts'] = __('Counts');
    return $defaults;
}
add_filter('manage_posts_columns', 'posts_columns_counts', 1);


function wpc_posts_custom_columns_counts($column_name, $id){
    if($column_name === 'wps_post_counts'){
        wpc_pingback_count('trackbacks'); echo "
"; wpc_pingback_count('pingbacks'); } } add_action('manage_posts_custom_column', 'wpc_posts_custom_columns_counts', 1, 2);