This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
comments_template_query_args filter
To use the `comments_template_query_args` filter, first you have to register it using `add_filter`. You can write this code into `functions.php` of your activated theme or in a custom WordPress Plugin.
We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function `weplugins_modify_comments_template_query_args_defaults` which takes 1 parameter and we registered using `add_filter`. The first parameter `comments_template_query_args` is the name of the hook, the second parameter `weplugins_modify_comments_template_query_args_defaults` is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook so you can use `remove_filter` to remove `comments_template_query_args` filter.
Parameters
-
$comment_args: (array) Array of WP_Comment_Query arguments.
‘orderby’ (string|array) Field(s) to order by.
‘order’ (string) Order of results. Accepts ‘ASC’ or ‘DESC’.
‘status’ (string) Comment status.
‘include_unapproved’ (array) Array of IDs or email addresses whose unapproved comments will be included in results.
‘post_id’ (int) ID of the post.
‘no_found_rows’ (bool) Whether to refrain from querying for found rows.
‘update_comment_meta_cache’ (bool) Whether to prime cache for comment meta.
‘hierarchical’ (bool|string) Whether to query for comments hierarchically.
‘offset’ (int) Comment offset.
‘number’ (int) Number of comments to fetch.
Below the 1 parameter is required to use this hook.
Live Example
Example 1: Basic Modification
Below is an example of how you can use this hook to modify the comment query args.
function weplugins_modify_comments_template_query_args_defaults($comment_args) { // Update the $comment_args variable according to your website requirements and return this variable. You can modify the $comment_args variable conditionally too if you want. return $comment_args; } // add the filter add_filter( "comments_template_query_args", "weplugins_modify_comments_template_query_args_defaults", 10, 1 );
Example 2: Removing the Hook
To remove a hook callback, use the example below.
remove_filter( "comments_template_query_args", "weplugins_modify_comments_template_query_args_defaults", 10, 1 );
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Example 3: Conditional Modification
Here is an example where we modify the comment query args conditionally based on certain parameters.
function weplugins_modify_comments_template_query_args_defaults($comment_args) { if (is_single()) { $comment_args['number'] = 5; // Limit to 5 comments on single posts. } return $comment_args; } // add the filter add_filter( "comments_template_query_args", "weplugins_modify_comments_template_query_args_defaults", 10, 1 );
Contact Us
If you’re having any trouble using this hook, please contact us and we’d be happy to assist you.
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.