[How to] Enable Comments for All Posts in WordPress Using PHP
I had a problem with my blog recently, that some plugin was disabling comments for older posts. Editing all posts one-by-one and enabling comments back is a pain I know. So, I wrote a simple function to run an SQL query to enable comments for all posts at once. Here are the steps:
Step 1. Open functions.php
in your theme directory and add the following code snippet.
Caution: If you mess up the code in your
functions.php
file, your site may become unstable. Only proceed if you know what you are doing. Please read my guide on how to insert code on functions.php file for further assistance.
function enable_comments_for_all(){ global $wpdb; $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_status = 'open'")); // Enable comments $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET ping_status = 'open'")); // Enable trackbacks } enable_comments_for_all();
Step 2. After you add this code to functions.php file, visit your site once. When you visit your site, functions.php
file will load and this snippet will enable comments for all posts on your blog.
Note:
functions.php
file runs everytime you visit your site unless you are using a caching plugin. In that case you may need purge your cache first. I also recommend clearing your browser cache before proceeding.
Step 3. After comments are enabled, you can remove the code from the functions file.
And that’s all. I hope this helps you out.
Visit our WordPress Tips archive page for more tips on WordPress.