<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>beer planet &#187; sql</title> <atom:link href="http://beerpla.net/tag/sql/feed/" rel="self" type="application/rss+xml" /><link>http://beerpla.net</link> <description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description> <lastBuildDate>Thu, 17 May 2012 22:50:53 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <atom:link rel='hub' href='http://beerpla.net/?pushpress=hub'/> <item><title>How To Diagnose And Fix Incorrect Post Comment Counts In WordPress</title><link>http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/</link> <comments>http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/#comments</comments> <pubDate>Sun, 21 Mar 2010 10:42:10 +0000</pubDate> <dc:creator>Artem Russakovskii</dc:creator> <category><![CDATA[Wordpress]]></category> <category><![CDATA[broken]]></category> <category><![CDATA[comment]]></category> <category><![CDATA[count]]></category> <category><![CDATA[database]]></category> <category><![CDATA[fix]]></category> <category><![CDATA[incorrect]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[query]]></category> <category><![CDATA[sql]]></category> <category><![CDATA[wp_comments]]></category> <category><![CDATA[wp_posts]]></category> <guid
isPermaLink="false">http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/</guid> <description><![CDATA[<p><img
style="margin: 0px 10px 10px 0px; display: inline" title="image" alt="image" align="left" src="http://beerpla.net/wp-content/uploads/517e9e45d3fa_2C92/image.png" width="222" height="189" /></p><h2>Introduction</h2><p>If your WordPress comment counts got messed up, whether because of a plugin (I&#039;m talking about you, DISQUS) or you messed with your database manually and did something wrong (yup, that&#039;s what I just did), fear not &#8211; I have a solution for you.</p><p>But first, a little background.</p><h2>Comment Counts In WordPress</h2><p>Here&#039;s how comment counts work in WP:</p><ul><li>Posts live in a table called <strong><em>wp_posts</em></strong> and each has an ID.</li><li>Comments reside in a table called <strong><em>wp_comments</em></strong>, each referring to an ID in wp_posts.</li><li>However, to make queries faster, the comment count is also cached in the wp_posts table, rather than getting calculated on every page load. <br
/>If this count ever gets out of sync with</li>...<div
class=clear></div> <a
href="http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/" class="read_more"><div
class=excerpt-end>Read the rest of this article &#187;</div></a></ul>]]></description> <content:encoded><![CDATA[<p><img
style="margin: 0px 10px 10px 0px; display: inline" title="image" alt="image" align="left" src="http://beerpla.net/wp-content/uploads/517e9e45d3fa_2C92/image.png" width="222" height="189" /></p><h2>Introduction</h2><p>If your WordPress comment counts got messed up, whether because of a plugin (I&#039;m talking about you, DISQUS) or you messed with your database manually and did something wrong (yup, that&#039;s what I just did), fear not &#8211; I have a solution for you.</p><p>But first, a little background.</p><h2>Comment Counts In WordPress</h2><p>Here&#039;s how comment counts work in WP:</p><ul><li>Posts live in a table called <strong><em>wp_posts</em></strong> and each has an ID.</li><li>Comments reside in a table called <strong><em>wp_comments</em></strong>, each referring to an ID in wp_posts.</li><li>However, to make queries faster, the comment count is also cached in the wp_posts table, rather than getting calculated on every page load. <br
/>If this count ever gets out of sync with the actual number of comments for some reason, WordPress, while still displaying all comments properly, will simply show the wrong count.</li></ul><h2>How To Find Out Which Posts Are Out Of Sync</h2><p>Fire up a MySQL shell or your favorite MySQL software (mine is <a
href="http://www.navicat.com" rel="nofollow">Navicat</a>) and run this query.</p><p>It assumes your database is called <strong><em>wordpress</em></strong> and the prefix is <strong><em>wp_</em></strong>, so adjust those accordingly.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td
class="code"><pre>SELECT wpp.id, wpp.post_title, wpp.comment_count, wpc.cnt
FROM wordpress.wp_posts wpp
LEFT JOIN
(SELECT comment_post_id AS c_post_id, count(*) AS cnt FROM wordpress.wp_comments
 WHERE comment_approved = 1 GROUP BY comment_post_id) wpc
ON wpp.id=wpc.c_post_id
WHERE wpp.post_type IN ('post', 'page')
      AND (wpp.comment_count!=wpc.cnt OR (wpp.comment_count != 0 AND wpc.cnt IS NULL));</pre></td></tr></table></div></p><p>The result of this query is a list of posts whose comment_counts differ from the actual number of comments associated with each of them.</p><p>The left count is the cached number, while the right one is the right one.</p><h2>How To Fix The Counts Automatically</h2><p><div
class="note"><div
class="notewarning">Please make a backup of your database before performing any altering queries such as the one below (I recommend mysqldump or the <a
href="http://lesterchan.net/wordpress/readme/wp-dbmanager.html" rel="nofollow">WP-DBManager plugin</a>).</div></div></p><p>The following query will recalculate and fix the comment counts for all posts that are out of sync (ones we just queried for above):</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td
class="code"><pre>UPDATE wordpress.wp_posts wpp
LEFT JOIN
(SELECT comment_post_id AS c_post_id, count(*) AS cnt FROM wordpress.wp_comments
 WHERE comment_approved = 1 GROUP BY comment_post_id) wpc
ON wpp.id=wpc.c_post_id
SET wpp.comment_count=wpc.cnt
WHERE wpp.post_type IN ('post', 'page')
      AND (wpp.comment_count!=wpc.cnt OR (wpp.comment_count != 0 AND wpc.cnt IS NULL));</pre></td></tr></table></div><p>I tested this approach on a few test cases but if you experience any problems, please do alert me in the comments and desribe your problem.</p><p>Happy WP hacking!</p><div
class='post_blob_1'>Online <a
href="http://www.test-king.com/exams/642-892.htm">642-892</a> training is the quickest way to pass <a
href="http://www.test-king.com/exams/642-642.htm">642-642</a> as well as <a
href="http://www.test-king.com/exams/640-863.htm">640-863</a> exams.</div><div
class="shr-bookmarks shr-bookmarks-expand"><ul
class="socials"><li
class="shr-twitter"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Diagnose+And+Fix+Incorrect+Post+Comment+Counts+In+Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li
class="shr-facebook"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Diagnose+And+Fix+Incorrect+Post+Comment+Counts+In+Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-googlebuzz"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Diagnose+And+Fix+Incorrect+Post+Comment+Counts+In+Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a></li><li
class="shr-reddit"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Diagnose+And+Fix+Incorrect+Post+Comment+Counts+In+Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a></li><li
class="shr-hackernews"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Diagnose+And+Fix+Incorrect+Post+Comment+Counts+In+Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=202&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a></li><li
class="shr-delicious"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Diagnose+And+Fix+Incorrect+Post+Comment+Counts+In+Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-stumbleupon"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Diagnose+And+Fix+Incorrect+Post+Comment+Counts+In+Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="shr-mail"> <a
href="http://www.shareaholic.com/api/share/?title=How%20To%20Diagnose%20And%20Fix%20Incorrect%20Post%20Comment%20Counts%20In%20Wordpress&amp;link=http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/&amp;notes=%0D%0AIntroduction%0D%0AIf%20your%20Wordpress%20comment%20counts%20got%20messed%20up%2C%20whether%20because%20of%20a%20plugin%20%28I%27m%20talking%20about%20you%2C%20DISQUS%29%20or%20you%20messed%20with%20your%20database%20manually%20and%20did%20something%20wrong%20%28yup%2C%20that%27s%20what%20I%20just%20did%29%2C%20fear%20not%20-%20I%20have%20a%20solution%20for%20you.%0D%0ABut%20first%2C%20a%20little%20background.%0D%0AComment&amp;short_link=http://bit.ly/cYF2ks&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=201&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a></li></ul><div
style="clear: both;"></div></div> Similar Posts:<ul><li><a
href="http://beerpla.net/2010/01/05/how-to-fix-intermittent-mysql-errcode-13-errors-on-windows/" rel="bookmark" title="January 5, 2010">How To Fix Intermittent MySQL Errcode 13 Errors On Windows</a></li><li><a
href="http://beerpla.net/2008/04/16/mysql-conference-liveblogging-mysql-performance-under-a-microscope-the-tobias-and-jay-show-wednesday-200pm/" rel="bookmark" title="April 16, 2008">MySQL Conference Liveblogging: MySQL Performance Under A Microscope: The Tobias And Jay Show (Wednesday 2:00PM)</a></li><li><a
href="http://beerpla.net/2008/04/15/mysql-conference-liveblogging-explain-demystified-tuesday-200p/" rel="bookmark" title="April 15, 2008">MySQL Conference Liveblogging: EXPLAIN Demystified (Tuesday 2:00PM)</a></li><li><a
href="http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/" rel="bookmark" title="May 11, 2009">[MySQL] Deleting/Updating Rows Common To 2 Tables &#8211; Speed And Slave Lag Considerations</a></li><li><a
href="http://beerpla.net/2008/03/25/navicat-for-mysql-bugs-filed/" rel="bookmark" title="March 25, 2008">Navicat For MySQL Bugs Filed</a></li></ul><p><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbeerpla.net%2F2010%2F03%2F21%2Fhow-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress%2F&amp;title=How%20To%20Diagnose%20And%20Fix%20Incorrect%20Post%20Comment%20Counts%20In%20WordPress" id="wpa2a_2"><img
src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/feed/</wfw:commentRss> <slash:comments>22</slash:comments> </item> </channel> </rss>
