{"id":1049,"date":"2012-06-17T03:45:37","date_gmt":"2012-06-17T02:45:37","guid":{"rendered":""},"modified":"2012-06-17T03:45:37","modified_gmt":"2012-06-17T02:45:37","slug":"query-wpmu-comment-count-for-a-list-of-blogs","status":"publish","type":"post","link":"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/","title":{"rendered":"Query WPMU Comment Count For A List Of Blogs"},"content":{"rendered":"

Recently created a new blog list function for PrimaryBlogger to allow users to see their current blogs in a lightbox iFrame, I thought it would also be nice to have a pending comment count on the page. get_blogs_of_user<\/code> does not support comment count or much else apart from giving you basic information about the users accessible blogs. I had to get creative due to us being on a Multi-DB install and $wpdb<\/code> would not use anything from the foreach loop created for use with get_blogs_of_user<\/code>. <\/p>\n

Here’s how I accomplished it:<\/p>\n

/* DB init */\nglobal $wpdb;\n \n/* Get current user details and declare variables to pull blog info */\n$current_user = wp_get_current_user();\n$user_id = $current_user->id;\n$user_blogs = get_blogs_of_user($user_id);\n \n/* Go through the list of blogs and create a list element for each */\nforeach ($user_blogs AS $user_blog) {\n \n/* Get the blog id for the current loop point */\n$blog_comment_id = $user_blog->userblog_id;\n \n/* MD5 hash the blog_id for use with Multi-DB, remove the last 30 characters - we only need the first 2! */\n$db_id = substr(md5($blog_comment_id), 0, -30);\n \n/* Create the SQL statement to pull the current loop point's blog pending comments and store it as $request */\n$sql = "SELECT COUNT(*) FROM db-prefix_{$db_id}.wp-prefix_{$blog_comment_id}_comments WHERE comment_approved = '0'";\n$request = $wpdb->get_var( $wpdb->prepare( $sql ) );<\/pre>\n

Then we just need to put it on the page…<\/p>\n

<a href="siteurl; ?>/wp-admin/edit-comments.php?comment_status=moderated" title=" Comments awaiting moderation" target="_blank"></a><\/pre>\n","protected":false},"excerpt":{"rendered":"

Recently created a new blog list function for PrimaryBlogger to allow users to see their current blogs in a lightbox iFrame, I thought it would also be nice to have a pending comment count on the page. get_blogs_of_user does not support comment count or much else apart from giving you basic information about the users […]<\/p>\n","protected":false},"author":2,"featured_media":3554,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false},"categories":[75],"tags":[129,192],"yoast_head":"\nQuery WPMU Comment Count For A List Of Blogs - E<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Query WPMU Comment Count For A List Of Blogs - E\" \/>\n<meta property=\"og:description\" content=\"Recently created a new blog list function for PrimaryBlogger to allow users to see their current blogs in a lightbox iFrame, I thought it would also be nice to have a pending comment count on the page. get_blogs_of_user does not support comment count or much else apart from giving you basic information about the users […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/\" \/>\n<meta property=\"og:site_name\" content=\"E\" \/>\n<meta property=\"article:published_time\" content=\"2012-06-17T02:45:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/static.ed.gs\/wp-content\/uploads\/2012\/08\/146H.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2048\" \/>\n\t<meta property=\"og:image:height\" content=\"1365\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ed\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ed\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/\",\"url\":\"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/\",\"name\":\"Query WPMU Comment Count For A List Of Blogs - E\",\"isPartOf\":{\"@id\":\"https:\/\/ed.gs\/#website\"},\"datePublished\":\"2012-06-17T02:45:37+00:00\",\"dateModified\":\"2012-06-17T02:45:37+00:00\",\"author\":{\"@id\":\"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ed.gs\/#website\",\"url\":\"https:\/\/ed.gs\/\",\"name\":\"E\",\"description\":\"automation, consultancy, project management, web\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ed.gs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628\",\"name\":\"Ed\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/ed.gs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g\",\"caption\":\"Ed\"},\"url\":\"https:\/\/ed.gs\/author\/ed\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Query WPMU Comment Count For A List Of Blogs - E","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/","og_locale":"en_GB","og_type":"article","og_title":"Query WPMU Comment Count For A List Of Blogs - E","og_description":"Recently created a new blog list function for PrimaryBlogger to allow users to see their current blogs in a lightbox iFrame, I thought it would also be nice to have a pending comment count on the page. get_blogs_of_user does not support comment count or much else apart from giving you basic information about the users […]","og_url":"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/","og_site_name":"E","article_published_time":"2012-06-17T02:45:37+00:00","og_image":[{"width":2048,"height":1365,"url":"https:\/\/static.ed.gs\/wp-content\/uploads\/2012\/08\/146H.jpg","type":"image\/jpeg"}],"author":"Ed","twitter_misc":{"Written by":"Ed","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/","url":"https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/","name":"Query WPMU Comment Count For A List Of Blogs - E","isPartOf":{"@id":"https:\/\/ed.gs\/#website"},"datePublished":"2012-06-17T02:45:37+00:00","dateModified":"2012-06-17T02:45:37+00:00","author":{"@id":"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ed.gs\/2012\/06\/17\/query-wpmu-comment-count-for-a-list-of-blogs\/"]}]},{"@type":"WebSite","@id":"https:\/\/ed.gs\/#website","url":"https:\/\/ed.gs\/","name":"E","description":"automation, consultancy, project management, web","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ed.gs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628","name":"Ed","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/ed.gs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g","caption":"Ed"},"url":"https:\/\/ed.gs\/author\/ed\/"}]}},"_links":{"self":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/posts\/1049"}],"collection":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/comments?post=1049"}],"version-history":[{"count":0,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/posts\/1049\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/media\/3554"}],"wp:attachment":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/media?parent=1049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/categories?post=1049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/tags?post=1049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}