Home / admin / Total pingbacks, trackbacks within admin post columns
  • Total pingbacks, trackbacks within admin post columns

    total-pingbacks-trackbacks-within-admin-post-columns

    Adding this snippet to the functions.php of your wordpress theme will add a new column called counts within the admin post listing. This column will display the total number of pingbacks, trackbacks for each post.

    wordpress snippet : PHP

    <>
    function commentCount($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;
    }
    
    
    add_filter('manage_posts_columns', 'posts_columns_counts', 1);
    add_action('manage_posts_custom_column', 'posts_custom_columns_counts', 1, 2);
    
    function posts_columns_counts($defaults){
        $defaults['wps_post_counts'] = __('Counts');
        return $defaults;
    }
    function posts_custom_columns_counts($column_name, $id){
    	if($column_name === 'wps_post_counts'){
    		commentCount('trackbacks'); echo "<br />";
    		commentCount('pingbacks');
              }
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    manage_posts_columns, comment_post, get_the_ID, the_ID, pingback, trackback, comment_ID, add_filter, add_action, __, _n, wp,

Build a better with wpsnipp.com

Weekly snippets, themes, deals coupons roundup.

x No Thanks