How to Disable Comments on Old Posts in WordPress

Home » Snippets » How to Disable Comments on Old Posts in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’ve been running your WordPress site for a while, you may have noticed that some of your older posts are still receiving comments. While it’s great to have an engaged audience, it can also be frustrating to manage comments on old posts that are no longer relevant. In this article, I’ll show you how to disable comments on old posts and keep your site organized and clutter-free.

					function wpturbo_disable_comments_on_old_posts() {
   // Get post types
   $post_types = get_post_types(array('public' => true), 'names');
   // Get current time and subtract number of days
   $days_ago = date_i18n( 'U' ) - ( 30 * 24 * 60 * 60 ); // 30 days ago
   
   foreach ( $post_types as $post_type ) {
      // Get all published posts within last 30 days
      $posts = get_posts(array(
         'post_type' => $post_type,
         'post_status' => 'publish',
         'date_query' => array(
            'before' => date_i18n( 'Y-m-d H:i:s', $days_ago ),
         ),
      ));
      
      if ( $posts ) {
         foreach ( $posts as $post ) {
            // Disable comments on each post
            update_post_meta( $post->ID, '_comments_open', false );
            update_post_meta( $post->ID, '_ping_status', 'closed' );
         }
      }
   }
}
add_action( 'wp', 'wpturbo_disable_comments_on_old_posts' );
				

The purpose of this code snippet is to disable comments and pingbacks on old WordPress posts. By default, WordPress allows comments on all posts unless they are manually disabled on a per-post basis. This can lead to a large number of comments on older posts that are no longer relevant, making it difficult to manage and moderate comments for current content.

This function performs the following actions:

  1. Retrieves all public post types in WordPress.
    $post_types = get_post_types(array('public' => true), 'names');
  2. Calculates the current time (in seconds) and subtracts the number of days before which comments should be disabled. In this example, it is set to 30 days.
    $days_ago = date_i18n( 'U' ) - ( 30 * 24 * 60 * 60 ); // 30 days ago
  3. Using the get_posts function, it retrieves all published posts for each post type within the specified time range.
    $posts = get_posts(array(
    'post_type' => $post_type,
    'post_status' => 'publish',
    'date_query' => array(
        'before' => date_i18n( 'Y-m-d H:i:s', $days_ago ),
    ),
    ));
  4. For each post retrieved, it uses the update_post_meta function to set the comments and pingbacks to closed (disabled).
    update_post_meta( $post->ID, '_comments_open', false );
    update_post_meta( $post->ID, '_ping_status', 'closed' );
  5. Finally, this function hooks into the wp action, which fires after WordPress finishes loading but before it starts rendering the page.

By disabling comments for older posts, website owners can ensure that their comment sections remain relevant and manageable, ultimately providing a better user experience.

Register an account to save your snippets or go Pro to get more features.