Title: WP_Comment_Query::parse_orderby
Published: April 23, 2015
Last modified: February 24, 2026

---

# WP_Comment_Query::parse_orderby( string $orderby ): string|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#wp--skip-link--target)

Parse and sanitize ‘orderby’ keys passed to the comment query.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#parameters)󠁿

 `$orderby`stringrequired

Alias for the field to order by.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#return)󠁿

 string|false Value to used in the ORDER clause. False otherwise.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#source)󠁿

    ```php
    protected function parse_orderby( $orderby ) {
    	global $wpdb;

    	$allowed_keys = array(
    		'comment_agent',
    		'comment_approved',
    		'comment_author',
    		'comment_author_email',
    		'comment_author_IP',
    		'comment_author_url',
    		'comment_content',
    		'comment_date',
    		'comment_date_gmt',
    		'comment_ID',
    		'comment_karma',
    		'comment_parent',
    		'comment_post_ID',
    		'comment_type',
    		'user_id',
    	);

    	if ( ! empty( $this->query_vars['meta_key'] ) ) {
    		$allowed_keys[] = $this->query_vars['meta_key'];
    		$allowed_keys[] = 'meta_value';
    		$allowed_keys[] = 'meta_value_num';
    	}

    	$meta_query_clauses = $this->meta_query->get_clauses();
    	if ( $meta_query_clauses ) {
    		$allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );
    	}

    	$parsed = false;
    	if ( $this->query_vars['meta_key'] === $orderby || 'meta_value' === $orderby ) {
    		$parsed = "$wpdb->commentmeta.meta_value";
    	} elseif ( 'meta_value_num' === $orderby ) {
    		$parsed = "$wpdb->commentmeta.meta_value+0";
    	} elseif ( 'comment__in' === $orderby ) {
    		$comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
    		$parsed      = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
    	} elseif ( in_array( $orderby, $allowed_keys, true ) ) {

    		if ( isset( $meta_query_clauses[ $orderby ] ) ) {
    			$meta_clause = $meta_query_clauses[ $orderby ];
    			$parsed      = sprintf( 'CAST(%s.meta_value AS %s)', esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
    		} else {
    			$parsed = "$wpdb->comments.$orderby";
    		}
    	}

    	return $parsed;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-comment-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-comment-query.php#L1184)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-comment-query.php#L1184-L1235)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#related)󠁿

| Uses | Description | 
| [esc_sql()](https://developer.wordpress.org/reference/functions/esc_sql/)`wp-includes/formatting.php` |

Escapes data for use in a MySQL query.

  |

| Used by | Description | 
| [WP_Comment_Query::get_comment_ids()](https://developer.wordpress.org/reference/classes/wp_comment_query/get_comment_ids/)`wp-includes/class-wp-comment-query.php` |

Used internally to get a list of comment IDs matching the query vars.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_comment_query/parse_orderby/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.2.0](https://developer.wordpress.org/reference/since/4.2.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_comment_query%2Fparse_orderby%2F)
before being able to contribute a note or feedback.