Forums

WordPress MU Sitewide Tags Pages
SWT comment counts (12 posts)

  1. dgodot
    Member
    Posted 3 months ago #

    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/

  2. Ron Rennick
    MultiSite Guru
    Posted 3 months ago #

    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.

  3. dgodot
    Member
    Posted 3 months ago #

    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?

  4. Ron Rennick
    MultiSite Guru
    Posted 3 months ago #

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

  5. dgodot
    Member
    Posted 3 months ago #

    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?

  6. mantish
    Member
    Posted 2 months ago #

    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;
    }
  7. samdlaw
    Member
    Posted 3 weeks ago #

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

  8. tunelab
    Member
    Posted 1 week ago #

    @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!

  9. Ron Rennick
    MultiSite Guru
    Posted 6 days ago #

    @tunelab - at the beginning of the function add

    if( is_main_site() ) {
    return $count;
    }
  10. samdlaw
    Member
    Posted 6 days ago #

    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.

  11. Ron Rennick
    MultiSite Guru
    Posted 2 days ago #

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

  12. mantish
    Member
    Posted 1 day ago #

    @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.

Reply

You must log in to post.

About this Plugin

About this Topic