Try this code:
WARNING: THIS IS JUST PSEUDOCODE!
$entries = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type IN ('post', 'page')");
foreach($entries as $entry)
{
$post_id = $entry->ID;
$comment_count = $wpdb->get_var("SELECT COUNT(*) AS comment_cnt FROM wp_comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
$wpdb->query("UPDATE wp_posts SET comment_count="$comment_count" WHERE ID = '$post_id'");
}
Or you might want to try solution from this page (although it’s not the proper way as you will add another query for every post)
Related Posts:
- Would switching to InnoDB from MyISAM improve performance of comments table?
- Recent comments from my blogs only
- What do these phpMyAdmin errors mean on my WordPress databaes?
- comment_post_ID 0 (cannot remove from dashboard)
- What for is the table “wp_commentmeta” exactly?
- Can I store my custom comment_type into wp_comments table?
- Using Disqus, how to stop storing comments in wp database?
- What are the additional fields in wp_comments used for?
- How to Delete Old Comments by Date?
- SQL query to delete comments older than 90 days?
- What causes the “max_user_connections” warning on WordPress frontend?
- Error establishing a database connection – with Debug Data
- migrate comments from old database to new database
- WordPress unresponsive after calling wp_update_comment()
- What is the correct/safe way to delete all comments?
- Add action on delete comments?
- Why is_wp_error() is not returning false even there’s no defined error
- SQL Error wp_commentmeta – incorrect table definition
- How can I get a list of comments by target
- Problems with comments callback (Argument #1 is not an array?)
- Log file reports SQL Error processing child comments
- Error establishing database connection – high traffic?
- Delete all one-word comments
- WordPress ‘comment counts’ got messed up, because of a plugin – DISQUS
- Control panel loads slowly due to 2 slow queries on comment table
- Trouble Migrating all data blog from WordPress.com to WordPress.org
- delete duplicate comments
- How to fix database error duplicate entry
- after wordpress update to 3.5+ i get many errors in plugin wpdb::prepare()
- anyone can help me to know whats going wrong? [closed]
- SELECT SQL_CALC_FOUND_ROWS with wordpress search
- What might be the reason of Couldn’t fetch mysqli_result on another domain?
- WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version [closed]
- Returning error upon comment being flagged as spam
- Comment form in wordpress theme returns a javascript alert
- I imported an restore an database and It shows #1067 – Invalid default value for ‘user_registered’
- PHP Warning: mysqli_query(): after updating my websites php from 5.6 to 7.2
- The function is deprecated; use mysql_real_escape_string() instead
- Comment form not working
- Hang Up Followed By Can’t select database
- Print output of Table Creation
- Execute multiple PHP Snippets causes error?
- R: Comment out block of code
- Error: Duplicate entry ‘0’ for key ‘PRIMARY’
- How to enable comments for pending and draft posts?
- Add comments from the admin panel?
- WordPress will not operate correctly
- Multiple Comment Moderators and Notifications
- importing data from non-wordpress mysql db
- function to remove ‘comments and trackbacks are closed’
- Add a drop down list to comment form?
- How can I change the label “Comments” to “Review” everywhere in the WP installation without translation
- Error: MySQL shutdown unexpectedly. XAMPP local conf running WordPress
- How to move comments form above comments list in wordpress
- if statement on database query
- How to execute mulitple statement SQL queries using $wpdb->query?
- How to add enctype to multipart/form-data to comment form?
- Comments view limited to 20 results – any way to increase to 50 or 100?
- Changing position of cancel_comment_reply_link and other elements of comment form
- Split a database will improve performance?
- Why do comment moderators need to have all create/edit/delete toboth posts and pages?
- How do I really disable comments?
- Migrating database / content of non-CMS site to WordPress
- Comment form connection to Gravity Forms
- Add classname comment template from functions.php
- Reject Comments Based on Author Email
- How do I find which articles are missing a featured image in The WordPress database?
- Disqus comments and pingback script – how to change the order?
- Is it possible to embed github gists in wordpress comments?
- Comments feed – Undefined named entity: ndash
- How to convert WordPress comments to bbPress replies [closed]
- Get declared variable in single.php to work in comments.php without re-declaring it [closed]
- What is causing wp-comments-post.php to redirect to the browser’s IP address?
- comments_number not displaying
- creating new field on mysql
- Insert Ads “into” comments section in Genesis Themework
- WordPress Recent Comments Widget exclude own comments
- How to get comment images stored as serialized comment meta
- Hide comments on specific pages, not just disable future comments
- Additional content every x comments
- How to display category name from commenter’s custom post
- What’s the difference with trash_comment and wp_trash_comment?
- How can i change the order of comments?
- Refresh individual comment text via API
- What is my error trying to create a new table in the database?
- How do I refresh “Post Last Modified Time”, as long as comment is updated to Approve status?
- Need to output comment_form() function inside a foreach loop
- Comments are in database, don’t show up in WordPress backend or frontend
- Comment field override
- Insert data from form to database
- How can I get $wpdb to show MySQL warnings?
- Badges for Guests based on their comment counts [closed]
- Populate custom post type/custom fields from an external database
- two wordpress sites, two themes, one database, same content
- Comments Template shows, but not the comments
- Force logged in users to provide more data when commenting
- How do I get the comments section to show up?
- By Default, Turn Comments Off for Pages & Leave Comments On for Posts
- Custom query_posts() parameter
- Getting an error when trying to migrate to DV server from Grid with Media Temple [closed]