Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use comments_clauses filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 25, 2022
5 minutes read

Ever wondered how you can filter the comment query clauses in WordPress? Let’s dive into the comments_clauses filter. This hook allows you to modify the comment query clauses, and it’s super useful for customizing how comments are fetched and displayed on your site.

To use the comments_clauses filter, you first need to register it using add_filter. You can add this code to your theme’s functions.php file or in a custom WordPress plugin. At WePlugins, we always prefer creating a custom plugin for such tasks to ensure nothing breaks when you update your theme.

In the example below, we define a function weplugins_modify_comments_clauses_defaults that takes two parameters, and we register this function with the add_filter. The comments_clauses is the name of the hook, weplugins_modify_comments_clauses_defaults is the name of the function to be called, the third parameter is the priority, and the last parameter is the number of arguments to pass to the function.

Sometimes, you may need to remove a registered hook, and you can do this using remove_filter to remove the comments_clauses filter.

Parameters

Below are the two parameters required to use this hook:

  • $clauses: (string[]) An associative array of comment query clauses.
  • $query: (WP_Comment_Query) Current instance of WP_Comment_Query (passed by reference).

Live Examples

Example 1: Basic Usage

This example demonstrates how to modify the comment clauses query:

function weplugins_modify_comments_clauses_defaults($clauses, $query) {
    // Update the $clauses variable according to your website requirements
    return $clauses;
}
// Add the filter
add_filter("comments_clauses", "weplugins_modify_comments_clauses_defaults", 10, 2);

Example 2: Conditional Clause Modification

In this example, we modify the clauses conditionally based on a custom condition:

function weplugins_modify_comments_clauses_conditionally($clauses, $query) {
    if (is_admin()) {
        // Modify clauses only if in admin area
        $clauses['where'] .= " AND comment_approved = '1'";
    }
    return $clauses;
}
// Add the filter
add_filter("comments_clauses", "weplugins_modify_comments_clauses_conditionally", 10, 2);

Example 3: Removing the Hook

Here’s how you can remove the previously added hook:

remove_filter("comments_clauses", "weplugins_modify_comments_clauses_defaults", 10, 2);

Ensure you provide the same callback function name, priority, and number of arguments while removing the hook callback.

Need Help?

If you’re having any trouble using this hook, feel free to contact us for customization services. We’re here to help!

Access Premium WordPress Plugins

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.