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?
- Using Disqus with headless WordPress setup?
- Comment Form Fields when user logged in
- Need to output comment_form() function inside a foreach loop
- How to get the current post ID in comments.php?
- How to add inside WordPress comments loop?
- How can I enable comments on category page but not home page?
- Fix wp_term_relationships slow query in get_posts
- stop url changing when user comments
- Difference between date and date_gmt
- I would like some help wth an SQL query to link posts with categories
- Disallowed Tag Present in AMP WordPress ()
- Error establishing a database connection after moving local wordpressmultisite from one directory to another
- Attaching author tag to the comment
- How do I edit Comments.php so I can add Schema markup to comment output?
- Where is the HTML-handler part in the wpdb class?
- Exporting Post ID, Post Title, Primary Category and Primary Category ID
- Featured image from custom to WordPress database
- error establishing a database connection 3
- Subcriber getting multiiple notifications for new comments
- Comments are in database, don’t show up in WordPress backend or frontend
- WordPress Database Query works in phpMyAdmin but not in the code
- How to prevent users/authors from seing IP/email of new commentators?
- Why are my threaded comments not quite working? [closed]
- Why can’t i come back to older working version of php after ugrade with error log on php 7.2 and 7.3
- Unable to view comments on my website
- “wordpress error in establishing database connection” and missing tables
- WordPress database error only while loading page for 1st time
- Limit Comment Line Count – Add Read More Button
- Comments stuck inside post edit, but not showing under the published posts
- Author name length character limit?
- How to map data on CSV to web pages?
- Change commenters email to display name
- Write html code in comments
- Check if comment author has url
- error establishing database connection (WAMP + filezilla)
- Comments/Discussion Not enabled on newly created posts/pages
- Comment field override
- How to make comment reply available only for login members of a certain user roles only?
- wp_mail doesnt work inside transition_comment_status
- Insert data from form to database
- How to create index (sql) to a meta_key?
- Using WordPress core pop-up comment function, doesn’t popup, maybe an elementor issue
- next comment id number wordpress
- Commenter username copies author username
- Comments counter only for Authors Posts
- Use the loop to check if DB record exists?
- Remove text on Comment count
- wp list all users who haven’t commented on a post
- How add custom text under comment form
- Keep comment author’s name on their comment even when their account is deleted
- Pre- Approved Comment Capability
- Delete comments function
- Migrate comments and likes wordpress.com to selfhosted site
- How to post one comment for all the translated posts
- How do i add comment fields in my page in WordPress
- Fire Social Annex Code on Comment Approval
- Counting user’s comments – efficient way?
- Search and Replace in Windows XAMPP site