How to ‘close comments’ on old WordPress posts without manual intervention for each posting

If you have a huge old WordPress blog with thousands of postings – especially if you have migrated it from a non-Wordpress platform – the older postings may be an attractive nuisance for spammers.

The simplest thing to do is to close comments on those postings.

The problem with this is that WordPress provides a button to enable Close comments on new postings after [N] days - but this only applies to new postings; and people being what they are there is much discussion of how this impacts SEO or doesn’t really work or discussion of plugins which have long since been superseded by core WP functionality that still does not address the matter.

Eventually I found two treatises on addressing the matter properly, one in SQL and another in PHP; I went with the SQL solution because the same article also included instructions to open everything back up again.

Go read the real article if you need to do this – and remember to back up first – but the short version of what you have to do is:

  • Settings > Discussion > Automatically close comments after N days
  • Compute N days ago and a date YYYY-MM-DD that reflects that date
  • run: UPDATE wp_posts SET comment_status = 'closed', ping_status = 'closed' WHERE post_date < 'YYYY-MM-DD' AND post_status = 'publish';
  • Revisit the matter in N days and re-run the command to finally shut the door.

But really, go read the whole article first.

 

One thought on “How to ‘close comments’ on old WordPress posts without manual intervention for each posting

  1. alecm Post author

    And the ‘undo’ command is something along the lines of:

    UPDATE wp_posts SET comment_status = ‘open’, ping_status = ‘open’ WHERE comment_status = ‘closed’ AND post_status = ‘publish’;

    Reply

Leave a Reply