WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. dgodot
    Member
    Posted 2 years 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
    Plugin Author

    Posted 2 years 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 2 years 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
    Plugin Author

    Posted 2 years ago #

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

  5. dgodot
    Member
    Posted 2 years 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 years 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 2 years ago #

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

  8. tunelab
    Member
    Posted 2 years 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
    Plugin Author

    Posted 2 years ago #

    @tunelab - at the beginning of the function add

    if( is_main_site() ) {
    return $count;
    }
  10. samdlaw
    Member
    Posted 2 years 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
    Plugin Author

    Posted 2 years ago #

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

  12. mantish
    Member
    Posted 2 years 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.

  13. coderguy
    Member
    Posted 2 years ago #

    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?

  14. samdlaw
    Member
    Posted 2 years ago #

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

  15. tunelab
    Member
    Posted 2 years ago #

    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.

  16. sortofme
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic