Viewing 5 replies - 1 through 5 (of 5 total)
  • 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

    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

    (@drlightman)

    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.

    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

    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

    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

    (@drlightman)

    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 🙁

    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.