Title: get_comments, type ?
Last modified: August 19, 2016

---

# get_comments, type ?

 *  [DrLightman](https://wordpress.org/support/users/drlightman/)
 * (@drlightman)
 * [16 years ago](https://wordpress.org/support/topic/get_comments-type/)
 * [http://codex.wordpress.org/Function_Reference/get_comments](http://codex.wordpress.org/Function_Reference/get_comments)
 * I see i can use “type”, but it’s not documented? I looked into the source code(
   wp-includes/comment.php:get_comments) but it seems “type” is not taken into account
   as a possibile argument.
 * Am I wrong?

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

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years ago](https://wordpress.org/support/topic/get_comments-type/#post-1495805)
 * Yes you’re incorrect, set the type to a valid comment type and it will work as
   expected..
    [http://core.trac.wordpress.org/browser/trunk/wp-includes/comment.php#L290](http://core.trac.wordpress.org/browser/trunk/wp-includes/comment.php#L290)
 * Line 290 – 291
 *     ```
       // Ignore this line - forum indent bug fix
       	elseif ( ! empty( $type ) )
       		$post_where .= $wpdb->prepare( 'comment_type = %s AND ', $type );
       ```
   
 * $type is extracted from the args array, arg keys become a variable with the same
   name, the value being that given arg’s value.
 * So feed the function a valid comment type and you’ll get what you’d expect to..
   😉
 *  Thread Starter [DrLightman](https://wordpress.org/support/users/drlightman/)
 * (@drlightman)
 * [16 years ago](https://wordpress.org/support/topic/get_comments-type/#post-1495867)
 * How is possibile that my get_comments function looks like this:
 *     ```
       /**
        * Retrieve a list of comments.
        *
        * The comment list can be for the blog as a whole or for an individual post.
        *
        * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt',
        * 'order', 'number', 'offset', and 'post_id'.
        *
        * @since 2.7.0
        * @uses $wpdb
        *
        * @param mixed $args Optional. Array or string of options to override defaults.
        * @return array List of comments.
        */
       function get_comments( $args = '' ) {
       	global $wpdb;
   
       	$defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
   
       	$args = wp_parse_args( $args, $defaults );
       	extract( $args, EXTR_SKIP );
   
       	// $args can be whatever, only use the args defined in defaults to compute the key
       	$key = md5( serialize( compact(array_keys($defaults)) )  );
       	$last_changed = wp_cache_get('last_changed', 'comment');
       	if ( !$last_changed ) {
       		$last_changed = time();
       		wp_cache_set('last_changed', $last_changed, 'comment');
       	}
       	$cache_key = "get_comments:$key:$last_changed";
   
       	if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
       		return $cache;
       	}
   
       	$post_id = absint($post_id);
   
       	if ( 'hold' == $status )
       		$approved = "comment_approved = '0'";
       	elseif ( 'approve' == $status )
       		$approved = "comment_approved = '1'";
       	elseif ( 'spam' == $status )
       		$approved = "comment_approved = 'spam'";
       	elseif ( 'trash' == $status )
       		$approved = "comment_approved = 'trash'";
       	else
       		$approved = "( comment_approved = '0' OR comment_approved = '1' )";
   
       	$order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
   
       	$orderby = 'comment_date_gmt';  // Hard code for now
   
       	$number = absint($number);
       	$offset = absint($offset);
   
       	if ( !empty($number) ) {
       		if ( $offset )
       			$number = 'LIMIT ' . $offset . ',' . $number;
       		else
       			$number = 'LIMIT ' . $number;
   
       	} else {
       		$number = '';
       	}
   
       	if ( ! empty($post_id) )
       		$post_where = $wpdb->prepare( 'comment_post_ID = %d AND', $post_id );
       	else
       		$post_where = '';
   
       	$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
       	wp_cache_add( $cache_key, $comments, 'comment' );
   
       	return $comments;
       }
       ```
   
 * Not even similiar to the one you linked. I thought it was dued to the fact I’m
   referring to MU but on WP 2.9.2 also I have that. (wp-includes/comment.php) around
   line 185.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years ago](https://wordpress.org/support/topic/get_comments-type/#post-1495918)
 * Apologies, appears there are changes in the newer version, and it’s missing from
   the 2.9 branch..
 * However, look at the following function..
    [http://core.trac.wordpress.org/browser/branches/2.9/wp-includes/comment.php#L575](http://core.trac.wordpress.org/browser/branches/2.9/wp-includes/comment.php#L575)
 *     ```
       seperate_comments()
       ```
   
 * Use `get_comments()` to grab the necessary comments, pass that into seperate 
   comments and it’ll split the array into comment types. Not a perfect solution,
   but the alternative would be write a custom query to pull what you want from 
   the DB..
 * eg.
 *     ```
       $comments_q = $wpdb->get_results( "SQL QUERY TO COMMENTS TABLE GOES HERE" );
       ```
   
 * [http://codex.wordpress.org/Function_Reference/wpdb_Class](http://codex.wordpress.org/Function_Reference/wpdb_Class)
 * This option would probably be better right now, because you could selectively
   grab just what you need, rather then grabbing all comments and filtering out 
   what you don’t want.
 *  Thread Starter [DrLightman](https://wordpress.org/support/users/drlightman/)
 * (@drlightman)
 * [16 years ago](https://wordpress.org/support/topic/get_comments-type/#post-1495940)
 * Thanks 🙂
 * By now I’ve modified the core file adding the “type” filter support, I hate doing
   that because with the next update I’d forget it for sure 🙁
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/get_comments-type/#post-1495979)
 * The new code will be in the next version though… 😉 so it won’t matter.. i can’t
   see another minor release(ie. 2.9.x) happening before 3.0 now.

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

The topic ‘get_comments, type ?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/get_comments-type/#post-1495979)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
