Title: WordPress hook for listing comments
Last modified: September 24, 2018

---

# WordPress hook for listing comments

 *  [kiruthikanaethra](https://wordpress.org/support/users/kiruthikanaethra/)
 * (@kiruthikanaethra)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/)
 * Hi,
    I have developed custom plugin.I need comment option for my plugin.I have
   passed some key value as meta key.In comment list, it displays all the comments
   for the particular page.But i want to display the comments based on the meta 
   key.Is this possible.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Radu Constantin](https://wordpress.org/support/users/raduconstantin/)
 * (@raduconstantin)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/#post-10718326)
 * Hi,
 * Could you give some more details about the changes that you made so far? Basically,
   as long as the args array uses the keys from here [https://codex.wordpress.org/Class_Reference/WP_Comment_Query#Default_Usage](https://codex.wordpress.org/Class_Reference/WP_Comment_Query#Default_Usage)
   everything should be fine when. Also, I think you can use `pre_get_comments` 
   hook to alter the comments results. Have you tried to use this?
 *  Thread Starter [kiruthikanaethra](https://wordpress.org/support/users/kiruthikanaethra/)
 * (@kiruthikanaethra)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/#post-10720174)
 * Hi
    Thanks for your reply.I have used pre_get_comments hook.But nothing changed.
   I have used this code in my plugin.Please tell me what is the wrong with this
   code. function action_pre_get_comments( $array ) { // make action magic happen
   here… }
 * // add the action
    add_action( ‘pre_get_comments’, ‘action_pre_get_comments’,
   10, 1 ); Thanks in advance.
    -  This reply was modified 7 years, 8 months ago by [kiruthikanaethra](https://wordpress.org/support/users/kiruthikanaethra/).
 *  [Radu Constantin](https://wordpress.org/support/users/raduconstantin/)
 * (@raduconstantin)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/#post-10721627)
 * Hi,
 * Can you try this code, please?
 *     ```
       add_action('pre_get_comments', function($query) {
       	$query->meta_query = new WP_Comment_Query();
   
       	$query->query_vars['orderby'] = 'my_meta_key';
   
       	$query->meta_query->parse_query_vars( $query->query_vars );
       });
       ```
   
 * I just tried to use something like this:
 *     ```
       add_action('pre_get_comments', function($query) {
       	$query->meta_query = new WP_Comment_Query();
   
       	$query->query_vars['orderby'] = 'user_id';
       	$query->query_vars['order'] = 'ASC';
   
       	$query->meta_query->parse_query_vars( $query->query_vars );
       });
       ```
   
 * and it works very well.
 *  Thread Starter [kiruthikanaethra](https://wordpress.org/support/users/kiruthikanaethra/)
 * (@kiruthikanaethra)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/#post-10721648)
 * Hi
    In my requirement is display the comments by its meta key.I have game_id 
   as comment_meta.When i pass the game_id then want to display that comments only.
   Can you please help me.I’m using the following code.But nothing happend. add_action(‘
   pre_get_comments’, function($query) { $query->meta_query = new WP_Comment_Query();
 *  $query->query_vars[‘game_id’] = 5;
    // $query->query_vars[‘order’] = ‘Desc’;
 *  $query->meta_query->parse_query_vars( $query->query_vars );
 *  [Radu Constantin](https://wordpress.org/support/users/raduconstantin/)
 * (@raduconstantin)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/#post-10721817)
 * Hi,
 * Can you send me the piece of code that you used to add the meta key to the comments,
   please?
 *  Thread Starter [kiruthikanaethra](https://wordpress.org/support/users/kiruthikanaethra/)
 * (@kiruthikanaethra)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/#post-10722044)
 * Hi
    This is the code im using to save metakey.Now i want to retrieve the value
   based on my meta key.
 *     ```
       add_action( 'comment_form_after_fields', 'pmg_comment_tut_fields' );
       function pmg_comment_tut_fields()
       {
           ?>
           <p class="comment-form-title">
   
               <input type="hidden" name="vote_id" value="<?= $_GET['voteid'];?>"id="pmg_comment_title" />
           </p>
           <?php
       }
       function save_comment_meta_data( $comment_id ) {
           add_comment_meta( $comment_id, 'extra_field', $_POST[ 'vote_id' ] );
       }
       add_action( 'comment_post', 'save_comment_meta_data' );
       add_filter('comment_post_redirect', 'redirect_after_comment');
       function redirect_after_comment($location)
       {
       return $_SERVER["HTTP_REFERER"];
       }
   
       add_action('pre_get_comments', function($query) {
               $query->meta_query = new WP_Comment_Query();
   
               $query->query_vars['vote_id'] = 5;
              // $query->query_vars['order'] = 'Desc';
   
               $query->meta_query->parse_query_vars( $query->query_vars );
       });
       ```
   
    -  This reply was modified 7 years, 8 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code fixed

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘WordPress hook for listing comments’ is closed to new replies.

## Tags

 * [comment-meta](https://wordpress.org/support/topic-tag/comment-meta/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 2 participants
 * Last reply from: [kiruthikanaethra](https://wordpress.org/support/users/kiruthikanaethra/)
 * Last activity: [7 years, 8 months ago](https://wordpress.org/support/topic/wordpress-hook-for-listing-comments/#post-10722044)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
