• I am trying to display accurate comment counts for posts that SWT has pulled in from child blogs.

    I found these threads in which the issue was addressed previously:

    I have implemented that suggestions in the first, newest thread by adding the following code to my child theme’s functions.php:

    function ds_sitewide_tags_get_comments_num($count) {
      global $blog_id, $wpdb, $post;
      	$tags_blog_id = get_site_option( 'tags_blog_id' );
      	if( !$tags_blog_id || $blog_id != $tags_blog_id ) return $count;
      	list( $post_blog_id, $post_id ) = explode( '.', $post->guid );
      	$base = $wpdb->get_blog_prefix( $post_blog_id );
      	$r = $wpdb->get_col( "SELECT comment_count FROM {$base}posts WHERE ID=$post_id" );
     	if( is_array( $r ) ) return $r[0] + $count;
      return $count;
    }
    add_filter('get_comments_number', 'ds_sitewide_tags_get_comments_num');

    I also commented out the line in SWT that turns off comments

    //$post->comment_status = 'closed';

    and changed the line in my theme that displays the comment count to dsader’s suggestion from the second, older thread:

    <a href="<?php comments_link(); ?>">Comments (<?php comments_number('0', '1', '%'); ?>)</a>

    However, comment counts for all posts from child blogs still show up as zero on my main site index (I have the main blog set as my tags blog.)

    Has anything changed in the last 3 months that would make this code not work? Or can anyone suggest ways to make it work?

    Thanks in advance!

    http://wordpress.org/extend/plugins/wordpress-mu-sitewide-tags/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Ron Rennick

    (@wpmuguru)

    Not in the last 3 months, no. The format of the guid changed in WP 3.1.3. Look at your DB to see the new format, so you know what to test with/for.

    Thread Starter dgodot

    (@dgodot)

    Hi Ron. I’m not very familiar with the WP database format — any clues as to where I would look and what I would be looking for?

    Plugin Author Ron Rennick

    (@wpmuguru)

    here’s the codex page on the DB schema: http://codex.wordpress.org/Database_Description

    Thread Starter dgodot

    (@dgodot)

    I really have no idea how to use the suggestion to look at the DB schema. I’m not knowledgeable enough to know what I would be looking for or how it would apply to this problem.

    Does anyone have any suggestions about how to modify the code quoted in my original post to make it work with the latest version of WP & the Sitewide Tags plugin?

    Hi dgodot,
    I made some changes to the function and now it’s working for me.
    Here’s the code:

    function ds_sitewide_tags_get_comments_num($count) {
      global $blog_id, $wpdb, $post;
    	$tags_blog_id = get_sitewide_tags_option( 'tags_blog_id' );
    	if( !$tags_blog_id || $blog_id != $tags_blog_id ) return $count;
    
    	//guid format is http://{blogid}.{postid}
    	list( $post_blog_id, $post_id ) = explode( '.', substr($post->guid, 7));
    
    	$wpdbobj = clone $wpdb;
    	$wpdb->blogid = $post_blog_id;
    	$wpdb->set_prefix( $wpdb->base_prefix );
    	$r = $wpdb->get_var( "SELECT comment_count FROM $wpdb->posts WHERE ID=$post_id" );
    	$wpdb = clone $wpdbobj;
    
    	if (is_null($r)) return 0;
    	return $r;
    }

    I’m n00b, can anyone tell me how to integrate the code and where all to make the changes.

    @mantish, using your code I do successfully pull the sub-blog comment count and display it on the main blog, however any articles actually posted on the main blog with comments show as 0 no matter how many there are. So in essence, it reverses what was there before.

    If anybody has a fix that gets both sub-blog and main blog comments correctly displaying on the main blog, it would be much appreciated!

    Plugin Author Ron Rennick

    (@wpmuguru)

    @tunelab – at the beginning of the function add

    if( is_main_site() ) {
    return $count;
    }

    Hi Ron,
    Can you please tell me where at what are exact changes. As I’m not much familiar with coding in wordpress, it is actually confusing. Your help will be highly appreciated. If you can mail me the updated code, even that will be a lot helpful.

    Plugin Author Ron Rennick

    (@wpmuguru)

    @samdlaw – my previous post was referring to the code that @mantish posted above.

    @samdlaw – you just have to copy the function into the functions.php file of your theme. Add the code posted by Ron, so it works properly.

    It would probably be more efficient to add a custom meta value for the comment count to each post in the main site. Every time a comment is submitted (or approved if need be), increment the count; and every time a comment is deleted, decrement the count.

    That way you wouldn’t have to query the DB for every comment count on the page.

    Update: denormalization isn’t always bad, you know?

    @mantish, @ron: I tried adding the code in Functions.php (by making a child theme so as not to mess up the core functionality)

    function ds_sitewide_tags_get_comments_num($count) {
      if( is_main_site() ) {
        return $count;
      }
      global $blog_id, $wpdb, $post;
    	$tags_blog_id = get_sitewide_tags_option( 'tags_blog_id' );
    	if( !$tags_blog_id || $blog_id != $tags_blog_id ) return $count;
    
    	//guid format is http://{blogid}.{postid}
    	list( $post_blog_id, $post_id ) = explode( '.', substr($post->guid, 7));
    
    	$wpdbobj = clone $wpdb;
    	$wpdb->blogid = $post_blog_id;
    	$wpdb->set_prefix( $wpdb->base_prefix );
    	$r = $wpdb->get_var( "SELECT comment_count FROM $wpdb->posts WHERE ID=$post_id" );
    	$wpdb = clone $wpdbobj;
    
    	if (is_null($r)) return 0;
    	return $r;
    }
    add_filter('get_comments_number', 'ds_sitewide_tags_get_comments_num');

    Also commented out //$post->comment_status = 'closed'; in sitewide-tags.php. Still no luck. I am using Suffusion theme.

    Unfortunately I didn’t get notified of the reply (or just didn’t see it?), but thanks for the additional help.

    Another unfortunate is that the latest code, even with Ron’s addition, doesn’t appear to work with 3.4.1.

    I’ve tried and nothing changes except the format of the comments_link, even with different themes.

    yikes!
    any viable solutions for this for WP3.4.1?
    Also, has anyone come across a solution, where I can pull in not only the comment count, but also the first few comments of a post through SWT? Been googling it but no luck…
    very much appreciated.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Plugin: WordPress MU Sitewide Tags Pages] SWT comment counts’ is closed to new replies.