I’ve been maintaining a company website that’s based on WordPress that has blog posts going back for years. Comments are disabled everywhere on the site as no one is moderating them and the posts are purely informational anyway and not meant for discussion.
However, recently there’s been an uptick in spam comments on older posts and attachments from before comments were disabled where apparently they are still enabled.
Going through each post and attachment to manually disable comments is not practical since there are hundreds to go through. What is the best way for me to automatically disable comments on all these older posts/attachment pages?
Thanks.
[ad_2]
One way would be to go to Settings -> Discussion and turn on “Users must be registered and logged in to comment” and then also disable “Anyone can register” in Settings -> General.
Alternatively if you feel comfortable querying the database (and made a backup first) you can close comments on all posts at once:
UPDATE wp_posts SET comment_status = ‘closed’;
Spam comments on media drive me f*kin mad… This will switch off comments for all posts, all pages and the second function all media. Paste them in functions.php
Refresh the screen. Then remove it again.
`function loop_through_all_posts_and_close_comments() {`
`global $wpdb;`
`//arguments for posts`
`$args = array(`
`’post_type’ => array(‘page’, ‘post’), //all posts`
`’post_status’ => ‘any’`
`);`
`//create loop`
`$query = new WP_Query($args);`
`if($query->have_posts()) { //check query has returned posts`
`while($query->have_posts()) { //loop through posts`
`$query->the_post(); //load post`
`$pid = $query->post->ID;`
`$wpdb->update( $wpdb->prefix . ‘posts’, array( ‘comment_status’ => ‘closed’ ), array( ‘ID’ => $pid ) );`
`}`
`}`
`wp_reset_postdata(); //clear post object data`
`}`
`add_action(‘init’, ‘loop_through_all_posts_and_close_comments’);`
`function loop_through_all_images_and_close_comments() {`
`global $wpdb;`
`//arguments for posts`
`$args = array(`
`’post_type’ => ‘attachment’,`
`’post_mime_type’ => ‘image’,`
`’post_status’ => ‘all’,`
`’posts_per_page’ => – 1,`
`);`
`//create loop`
`$query = new WP_Query($args);`
`if($query->have_posts()) { //check query has returned posts`
`while($query->have_posts()) { //loop through posts`
`$query->the_post(); //load post`
`$pid = $query->post->ID;`
`$wpdb->update( $wpdb->prefix . ‘posts’, array( ‘comment_status’ => ‘closed’ ), array( ‘ID’ => $pid ) );`
`}`
`}`
`wp_reset_postdata(); //clear post object data`
`}`
`add_action(‘init’, ‘loop_through_all_images_and_close_comments’);`