WP_Comment_Query::__construct( string|array $query = '' )

Constructor.


Description

Sets up the comment query, based on the query vars passed.


Parameters

$query

(Optional) Array or query string of comment query parameters. <br>

  • 'author_email'
    (string) Comment author email address. <br>
  • 'author_url'
    (string) Comment author URL. <br>
  • 'author__in'
    (int[]) Array of author IDs to include comments for. <br>
  • 'author__not_in'
    (int[]) Array of author IDs to exclude comments for. <br>
  • 'comment__in'
    (int[]) Array of comment IDs to include. <br>
  • 'comment__not_in'
    (int[]) Array of comment IDs to exclude. <br>
  • 'count'
    (bool) Whether to return a comment count (true) or array of comment objects (false). Default false.<br>
  • 'date_query'
    (array) Date query clauses to limit comments by. See WP_Date_Query.<br> Default null.<br>
  • 'fields'
    (string) Comment fields to return. Accepts 'ids' for comment IDs only or empty for all fields. <br>
  • 'include_unapproved'
    (array) Array of IDs or email addresses of users whose unapproved comments will be returned by the query regardless of $status. <br>
  • 'karma'
    (int) Karma score to retrieve matching comments for.<br> <br>
  • 'meta_key'
    (string|string[]) Meta key or keys to filter by.<br>
  • 'meta_value'
    (string|string[]) Meta value or values to filter by.<br>
  • 'meta_compare'
    (string) MySQL operator used for comparing the meta value.<br> See WP_Meta_Query::__construct() for accepted values and default value.<br>
  • 'meta_compare_key'
    (string) MySQL operator used for comparing the meta key.<br> See WP_Meta_Query::__construct() for accepted values and default value.<br>
  • 'meta_type'
    (string) MySQL data type that the meta_value column will be CAST to for comparisons.<br> See WP_Meta_Query::__construct() for accepted values and default value.<br>
  • 'meta_type_key'
    (string) MySQL data type that the meta_key column will be CAST to for comparisons.<br> See WP_Meta_Query::__construct() for accepted values and default value.<br>
  • 'meta_query'
    (array) An associative array of WP_Meta_Query arguments.<br> See WP_Meta_Query::__construct() for accepted values.<br>
  • 'number'
    (int) Maximum number of comments to retrieve.<br> Default empty (no limit).<br>
  • 'paged'
    (int) When used with $number, defines the page of results to return.<br> When used with $offset, $offset takes precedence. Default 1.<br>
  • 'offset'
    (int) Number of comments to offset the query. Used to build LIMIT clause. Default 0.<br>
  • 'no_found_rows'
    (bool) Whether to disable the SQL_CALC_FOUND_ROWS query.<br> Default: true.<br>
  • 'orderby'
    (string|array) Comment status or array of statuses. To use 'meta_value' or 'meta_value_num', $meta_key must also be defined.<br> To sort by a specific $meta_query clause, use that clause's array key. Accepts:<br>
    • 'comment_agent'<br>
    • 'comment_approved'<br>
    • 'comment_author'<br>
    • 'comment_author_email'<br>
    • 'comment_author_IP'<br>
    • 'comment_author_url'<br>
    • 'comment_content'<br>
    • 'comment_date'<br>
    • 'comment_date_gmt'<br>
    • 'comment_ID'<br>
    • 'comment_karma'<br>
    • 'comment_parent'<br>
    • 'comment_post_ID'<br>
    • 'comment_type'<br>
    • 'user_id'<br>
    • 'comment__in'<br>
    • 'meta_value'<br>
    • 'meta_value_num'<br>
    • The value of $meta_key<br>
    • The array keys of $meta_query<br>
    • false, an empty array, or 'none' to disable ORDER BY clause.<br> Default: 'comment_date_gmt'.<br>
  • 'order'
    (string) How to order retrieved comments. Accepts 'ASC', 'DESC'.<br> Default: 'DESC'.<br>
  • 'parent'
    (int) Parent ID of comment to retrieve children of.<br> <br>
  • 'parent__in'
    (int[]) Array of parent IDs of comments to retrieve children for.<br> <br>
  • 'parent__not_in'
    (int[]) Array of parent IDs of comments *not* to retrieve children for. <br>
  • 'post_author__in'
    (int[]) Array of author IDs to retrieve comments for.<br> <br>
  • 'post_author__not_in'
    (int[]) Array of author IDs *not* to retrieve comments for.<br> <br>
  • 'post_id'
    (int) Limit results to those affiliated with a given post ID.<br> Default 0.<br>
  • 'post__in'
    (int[]) Array of post IDs to include affiliated comments for.<br> <br>
  • 'post__not_in'
    (int[]) Array of post IDs to exclude affiliated comments for.<br> <br>
  • 'post_author'
    (int) Post author ID to limit results by. <br>
  • 'post_status'
    (string|string[]) Post status or array of post statuses to retrieve affiliated comments for. Pass 'any' to match any value.<br> <br>
  • 'post_type'
    (string|string[]) Post type or array of post types to retrieve affiliated comments for. Pass 'any' to match any value. <br>
  • 'post_name'
    (string) Post name to retrieve affiliated comments for.<br> <br>
  • 'post_parent'
    (int) Post parent ID to retrieve affiliated comments for.<br> <br>
  • 'search'
    (string) Search term(s) to retrieve matching comments for.<br> <br>
  • 'status'
    (string|array) Comment statuses to limit results by. Accepts an array or space/comma-separated list of 'hold' (comment_status=0), 'approve' (comment_status=1), 'all', or a custom comment status. Default 'all'.<br>
  • 'type'
    (string|string[]) Include comments of a given type, or array of types.<br> Accepts 'comment', 'pings' (includes 'pingback' and 'trackback'), or any custom type string. <br>
  • 'type__in'
    (string[]) Include comments from a given array of comment types.<br> <br>
  • 'type__not_in'
    (string[]) Exclude comments from a given array of comment types.<br> <br>
  • 'user_id'
    (int) Include comments for a specific user ID. <br>
  • 'hierarchical'
    (bool|string) Whether to include comment descendants in the results.<br>
  • 'threaded' returns a tree, with each comment's children stored in a children property on the WP_Comment object.<br>
  • 'flat' returns a flat array of found comments plus their children.<br>
  • Boolean false leaves out descendants.<br> The parameter is ignored (forced to false) when $fields is 'ids' or 'counts'. Accepts 'threaded', 'flat', or false. Default: false.<br>
  • 'cache_domain'
    (string) Unique cache key to be produced when this query is stored in an object cache. Default is 'core'.<br>
  • 'update_comment_meta_cache'
    (bool) Whether to prime the metadata cache for found comments.<br> Default true.<br>
  • 'update_comment_post_cache'
    (bool) Whether to prime the cache for comment posts.<br> Default false.<br>
  • Default value: ''


    Source

    File: wp-includes/class-wp-comment-query.php

    	public function __construct( $query = '' ) {
    		$this->query_var_defaults = array(
    			'author_email' => '',
    			'author_url' => '',
    			'author__in' => '',
    			'author__not_in' => '',
    			'include_unapproved' => '',
    			'fields' => '',
    			'ID' => '',
    			'comment__in' => '',
    			'comment__not_in' => '',
    			'karma' => '',
    			'number' => '',
    			'offset' => '',
    			'no_found_rows' => true,
    			'orderby' => '',
    			'order' => 'DESC',
    			'paged' => 1,
    			'parent' => '',
    			'parent__in' => '',
    			'parent__not_in' => '',
    			'post_author__in' => '',
    			'post_author__not_in' => '',
    			'post_ID' => '',
    			'post_id' => 0,
    			'post__in' => '',
    			'post__not_in' => '',
    			'post_author' => '',
    			'post_name' => '',
    			'post_parent' => '',
    			'post_status' => '',
    			'post_type' => '',
    			'status' => 'all',
    			'type' => '',
    			'type__in' => '',
    			'type__not_in' => '',
    			'user_id' => '',
    			'search' => '',
    			'count' => false,
    			'meta_key' => '',
    			'meta_value' => '',
    			'meta_query' => '',
    			'date_query' => null, // See WP_Date_Query
    			'hierarchical' => false,
    			'cache_domain' => 'core',
    			'update_comment_meta_cache' => true,
    			'update_comment_post_cache' => false,
    		);
    
    		if ( ! empty( $query ) ) {
    			$this->query( $query );
    		}
    	}
    


    Changelog

    Changelog
    Version Description
    5.3.0 Introduced the $meta_type_key argument.
    5.1.0 Introduced the $meta_compare_key argument.
    4.9.0 Introduced the $paged argument.
    4.6.0 Introduced the $cache_domain argument.
    4.5.0 Introduced the $author_url argument.
    4.4.0 Order by comment__in was added. $update_comment_meta_cache, $no_found_rows, $hierarchical, and $update_comment_post_cache were added.
    4.2.0 Introduced.