How to Exclude a Category from Turning Off Comments Automatically in WordPress

Home » Snippets » How to Exclude a Category from Turning Off Comments Automatically 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

As a website owner, do you have certain categories that you don’t want users to leave comments on? By default, WordPress allows comments on all posts and pages, regardless of their category. However, if you want to exclude a specific category from this rule and turn off comments automatically, there’s a simple solution for that. In this article, we’ll show you step-by-step how to exclude a category from allowing comments and turn off comments automatically for all posts in that category.

					function wpturbo_exclude_category_from_comment_turnoff( $open, $post_id ) {
  $exclude_category_ids = array(1, 2, 3); // replace with IDs of categories to exclude
  $post_categories = wp_get_post_categories( $post_id );
  $category_intersect = array_intersect( $exclude_category_ids, $post_categories );

  if ( ! empty( $category_intersect ) ) {
    return true; // comments turned on for posts in excluded categories
  }

  return $open; // comments turned off for all other posts
}
add_filter( 'comments_open', 'wpturbo_exclude_category_from_comment_turnoff', 10, 2 );
				

The code snippet above provides a solution to automatically turn off comments for all posts except those belonging to specified categories. The first step is to define an array of category IDs to exclude from turning off comments for their posts. Replace the values in the $exclude_category_ids array with the IDs of the desired categories.

Next, we utilize the wp_get_post_categories() function to get an array of category IDs for the current post. We then use the array_intersect() function to find the intersection of category IDs between the $exclude_category_ids array and the $post_categories array. If the resulting array is not empty, it means that the post belongs to one of the excluded categories. In this case, comments should be turned on for this post.

We return true to enable comments for excluded categories and $open for all other post categories. Returning $open is the default behavior when the comment status is unspecified, so it disables comments for most posts by default.

Finally, we apply the solution using the add_filter() function. We pass two parameters to the wpturbo_exclude_category_from_comment_turnoff function – $open and $post_id. comments_open is the filter we use to turn off comments for all posts, except those in the excluded categories. The last parameter (2) indicates that our callback function takes two arguments – $open and $post_id. This function uses them to check if comments should be turned on or off for this post. The add_filter() function will return either true to enable comments for excluded categories or $open to disable comments for all other posts.

Overall, this code snippet is a powerful tool for managing comments on your WordPress site. It enables you to exclude specific categories from automatically having their comments turned off, providing you with greater flexibility and control over comment settings.

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