Title: WP_Term_Query::__construct
Published: August 16, 2016
Last modified: May 20, 2026

---

# WP_Term_Query::__construct( string|array $query = '' )

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#user-contributed-notes)

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

Constructor.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#description)󠁿

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

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

 `$query`string|arrayoptional

Array or query string of term query parameters.

 * `taxonomy` string|string[]
 * Taxonomy name, or array of taxonomy names, to which results should be limited.
 * `object_ids` int|int[]
 * Object ID, or array of object IDs. Results will be limited to terms associated
   with these objects.
 * `orderby` string
 * Field(s) to order terms by. Accepts:
    - Term fields (`'name'`, `'slug'`, `'term_group'`, `'term_id'`, `'id'`, `'description'`,`'
      parent'`, `'term_order'`). Unless `$object_ids` is not empty, `'term_order'`
      is treated the same as `'term_id'`.
    - `'count'` to use the number of objects associated with the term.
    - `'include'` to match the `'order'` of the `$include` param.
    - `'slug__in'` to match the `'order'` of the `$slug` param.
    - `'meta_value'`
    - `'meta_value_num'`.
    - The value of `$meta_key`.
    - The array keys of `$meta_query`.
    - `'none'` to omit the ORDER BY clause.
 *  Default `'name'`.
 * `order` string
 * Whether to order terms in ascending or descending order.
    Accepts `'ASC'` (ascending)
   or `'DESC'` (descending). Default `'ASC'`.
 * `hide_empty` bool|int
 * Whether to hide terms not assigned to any posts. Accepts `1|true` or `0|false`.
   Default `1|true`.
 * `include` int[]|string
 * Array or comma/space-separated string of term IDs to include.
 * `exclude` int[]|string
 * Array or comma/space-separated string of term IDs to exclude.
    If `$include` 
   is non-empty, `$exclude` is ignored.
 * `exclude_tree` int[]|string
 * Array or comma/space-separated string of term IDs to exclude along with all of
   their descendant terms. If `$include` is non-empty, `$exclude_tree` is ignored.
 * `number` int|string
 * Maximum number of terms to return. Accepts `''|0` (all) or any positive number.
   Default `''|0` (all). Note that `$number` may not return accurate results when
   coupled with `$object_ids`.
    See #41796 for details.
 * `offset` int
 * The number by which to offset the terms query.
 * `fields` string
 * Term fields to query for. Accepts:
    - `'all'` Returns an array of complete term objects (`WP_Term[]`).
    - `'all_with_object_id'` Returns an array of term objects with the `'object_id'`
      param (`WP_Term[]`). Works only when the `$object_ids` parameter is populated.
    - `'ids'` Returns an array of term IDs (`int[]`).
    - `'tt_ids'` Returns an array of term taxonomy IDs (`int[]`).
    - `'names'` Returns an array of term names (`string[]`).
    - `'slugs'` Returns an array of term slugs (`string[]`).
    - `'count'` Returns the number of matching terms (`int`).
    - `'id=>parent'` Returns an associative array of parent term IDs, keyed by term
      ID (`int[]`).
    - `'id=>name'` Returns an associative array of term names, keyed by term ID (`
      string[]`).
    - `'id=>slug'` Returns an associative array of term slugs, keyed by term ID (`
      string[]`).
 *  Default `'all'`.
 * `name` string|string[]
 * Name or array of names to return term(s) for.
 * `slug` string|string[]
 * Slug or array of slugs to return term(s) for.
 * `term_taxonomy_id` int|int[]
 * Term taxonomy ID, or array of term taxonomy IDs, to match when querying terms.
 * `hierarchical` bool
 * Whether to include terms that have non-empty descendants (even if `$hide_empty`
   is set to true). Default true.
 * `search` string
 * Search criteria to match terms. Will be SQL-formatted with wildcards before and
   after.
 * `name__like` string
 * Retrieve terms with criteria by which a term is LIKE `$name__like`.
 * `description__like` string
 * Retrieve terms where the description is LIKE `$description__like`.
 * `pad_counts` bool
 * Whether to pad the quantity of a term’s children in the quantity of each term’s"
   count" object variable. Default false.
 * `get` string
 * Whether to return terms regardless of ancestry or whether the terms are empty.
   Accepts `'all'` or `''` (disabled). Default `''`.
 * `child_of` int
 * Term ID to retrieve child terms of. If multiple taxonomies are passed, `$child_of`
   is ignored. Default 0.
 * `parent` int
 * Parent term ID to retrieve direct-child terms of.
 * `childless` bool
 * True to limit results to terms that have no children.
    This parameter has no 
   effect on non-hierarchical taxonomies. Default false.
 * `cache_domain` string
 * Unique cache key to be produced when this query is stored in an object cache.
   Default `'core'`.
 * `cache_results` bool
 * Whether to cache term information. Default true.
 * `update_term_meta_cache` bool
 * Whether to prime meta caches for matched terms. Default true.
 * `meta_key` string|string[]
 * Meta key or keys to filter by.
 * `meta_value` string|string[]
 * Meta value or values to filter by.
 * `meta_compare` string
 * MySQL operator used for comparing the meta value.
    See [WP_Meta_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_meta_query/__construct/)
   for accepted values and default value.
 * `meta_compare_key` string
 * MySQL operator used for comparing the meta key.
    See [WP_Meta_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_meta_query/__construct/)
   for accepted values and default value.
 * `meta_type` string
 * MySQL data type that the meta_value column will be CAST to for comparisons.
    
   See [WP_Meta_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_meta_query/__construct/)
   for accepted values and default value.
 * `meta_type_key` string
 * MySQL data type that the meta_key column will be CAST to for comparisons.
    See
   [WP_Meta_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_meta_query/__construct/)
   for accepted values and default value.
 * `meta_query` array
 * An associative array of [WP_Meta_Query](https://developer.wordpress.org/reference/classes/wp_meta_query/)
   arguments.
    See [WP_Meta_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_meta_query/__construct/)
   for accepted values.

Default:`''`

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

    ```php
    public function __construct( $query = '' ) {
    	$this->query_var_defaults = array(
    		'taxonomy'               => null,
    		'object_ids'             => null,
    		'orderby'                => 'name',
    		'order'                  => 'ASC',
    		'hide_empty'             => true,
    		'include'                => array(),
    		'exclude'                => array(),
    		'exclude_tree'           => array(),
    		'number'                 => '',
    		'offset'                 => '',
    		'fields'                 => 'all',
    		'name'                   => '',
    		'slug'                   => '',
    		'term_taxonomy_id'       => '',
    		'hierarchical'           => true,
    		'search'                 => '',
    		'name__like'             => '',
    		'description__like'      => '',
    		'pad_counts'             => false,
    		'get'                    => '',
    		'child_of'               => 0,
    		'parent'                 => '',
    		'childless'              => false,
    		'cache_domain'           => 'core',
    		'cache_results'          => true,
    		'update_term_meta_cache' => true,
    		'meta_query'             => '',
    		'meta_key'               => '',
    		'meta_value'             => '',
    		'meta_type'              => '',
    		'meta_compare'           => '',
    	);

    	if ( ! empty( $query ) ) {
    		$this->query( $query );
    	}
    }
    ```

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

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

| Uses | Description | 
| [WP_Term_Query::query()](https://developer.wordpress.org/reference/classes/wp_term_query/query/)`wp-includes/class-wp-term-query.php` |

Sets up the query and retrieves the results.

  |

| Used by | Description | 
| [_wp_build_title_and_description_for_taxonomy_block_template()](https://developer.wordpress.org/reference/functions/_wp_build_title_and_description_for_taxonomy_block_template/)`wp-includes/block-template-utils.php` |

Builds the title and description of a taxonomy-specific template based on the underlying entity referenced.

  | 
| [WP_REST_Term_Search_Handler::search_items()](https://developer.wordpress.org/reference/classes/wp_rest_term_search_handler/search_items/)`wp-includes/rest-api/search/class-wp-rest-term-search-handler.php` |

Searches terms for a given search request.

  | 
| [WP_Sitemaps_Taxonomies::get_url_list()](https://developer.wordpress.org/reference/classes/wp_sitemaps_taxonomies/get_url_list/)`wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php` |

Gets a URL list for a taxonomy sitemap.

  | 
| [WP_Tax_Query::transform_query()](https://developer.wordpress.org/reference/classes/wp_tax_query/transform_query/)`wp-includes/class-wp-tax-query.php` |

Transforms a single query, from one field to another.

  | 
| [get_terms()](https://developer.wordpress.org/reference/functions/get_terms/)`wp-includes/taxonomy.php` |

Retrieves the terms in a given taxonomy or list of taxonomies.

  |

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

| Version | Description | 
| [6.4.0](https://developer.wordpress.org/reference/since/6.4.0/) | Introduced the `'cache_results'` parameter. | 
| [5.3.0](https://developer.wordpress.org/reference/since/5.3.0/) | Introduced the `'meta_type_key'` parameter. | 
| [5.1.0](https://developer.wordpress.org/reference/since/5.1.0/) | Introduced the `'meta_compare_key'` parameter. | 
| [4.9.0](https://developer.wordpress.org/reference/since/4.9.0/) | Added `'slug__in'` support for `'orderby'`. | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced `'object_ids'` parameter. | 
| [4.6.0](https://developer.wordpress.org/reference/since/4.6.0/) | Introduced. |

[Show 1 more](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#)

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#comment-content-2500)
 2.    [bucketpress](https://profiles.wordpress.org/bucketpress/)  [  9 years ago  ](https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#comment-2500)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%23comment-2500)
     Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%23comment-2500)
 4.  @jmarceli ,
 5.  If we wanted to limit the query to specific term ids we would use the ‘include’
     or ‘exclude’ parameters instead. ‘object_ids’ are useful for cases such as when
     we want to return terms associated with a set of posts. In such a case we might
     want to do the following in a taxonomy archive:
 6.      ```php
         $someposts = get_posts(
         	array(
         		'post_type' => 'custom_post_type',
         		'posts_per_page' => -1,
         		'fields' => 'ids', // return an array of ids
         		'tax_query' => array(
         			array(
         				'taxonomy' => get_query_var('taxonomy'),
         				'field' => 'slug',
         				'terms' => $term_slug = get_queried_object()->slug,
         			)
         		)
         	)
         );
         ```
     
 7.  And then call this:
 8.      ```php
         $somepoststerms = get_terms(
         	array(
         		'taxonomy' => 'custom_tax',
         		'object_ids' => $someposts,
         	)
         );
         ```
     
 9.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%3Freplytocom%3D2500%23feedback-editor-2500)
 10.  [Skip to note 5 content](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#comment-content-4093)
 11.   Anonymous User  [  6 years ago  ](https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#comment-4093)
 12. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%23comment-4093)
     Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%23comment-4093)
 13. Note the inconsistency between 0 here and -1 on many other methods to return all
     the results. Also passing e negative number automatically convert to positive,
     so -1 means 1, further confusion.
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%3Freplytocom%3D4093%23feedback-editor-4093)
 15.  [Skip to note 6 content](https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/?output_format=md#comment-content-2442)
 16.   [jmarceli](https://profiles.wordpress.org/jmarceli/)  [  9 years ago  ](https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#comment-2442)
 17. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%23comment-2442)
     Vote results for this note: -10[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%23comment-2442)
 18. `'object_ids'` query parameter should be replaced with an object name, example
     for **get_terms**:
 19.     ```php
         get_terms([
             'taxonomy' => 'some_name',
             'term_ids' => [12,13], // NOT 'object_ids' => [12,13]
             'fields' => 'names',
           ])
         ```
     
 20.  * The `include` parameter accepts term IDs.
      * [Corey Salzano](https://profiles.wordpress.org/salzano/) [4 years ago](https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#comment-5603)
      * `'object_ids'` means posts (objects) associated to terms, results will be limited
        to terms associated with these posts. `'term_ids'` wouldn’t be a replacement
        for that.
      * [aefmind](https://profiles.wordpress.org/aefmind/) [4 years ago](https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#comment-5765)
      * For those reading _“Unless $object\_ids is not empty, ‘term\_order’ is treated
        the same as ‘term\_id’.”_ and getting confused on the double negatives, this
        can be more easily read as: If `$object_ids` is empty, `term_order` is treated
        the same as `term_id`.
      * [James Amner](https://profiles.wordpress.org/jdamner/) [2 years ago](https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#comment-7102)
 21.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_term_query%2F__construct%2F%3Freplytocom%3D2442%23feedback-editor-2442)

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