wp/wp-includes/class-wp-term-query.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    92 	 * @param string|array $query {
    92 	 * @param string|array $query {
    93 	 *     Optional. Array or query string of term query parameters. Default empty.
    93 	 *     Optional. Array or query string of term query parameters. Default empty.
    94 	 *
    94 	 *
    95 	 *     @type string|array $taxonomy               Taxonomy name, or array of taxonomies, to which results should
    95 	 *     @type string|array $taxonomy               Taxonomy name, or array of taxonomies, to which results should
    96 	 *                                                be limited.
    96 	 *                                                be limited.
    97 	 *     @type int|array    $object_ids             Optional. Object ID, or array of object IDs. Results will be
    97 	 *     @type int|int[]    $object_ids             Optional. Object ID, or array of object IDs. Results will be
    98 	 *                                                limited to terms associated with these objects.
    98 	 *                                                limited to terms associated with these objects.
    99 	 *     @type string       $orderby                Field(s) to order terms by. Accepts:
    99 	 *     @type string       $orderby                Field(s) to order terms by. Accepts:
   100 	 *                                                - term fields ('name', 'slug', 'term_group', 'term_id', 'id',
   100 	 *                                                * term fields ('name', 'slug', 'term_group', 'term_id', 'id',
   101 	 *                                                  'description', 'parent', 'term_order'). Unless `$object_ids`
   101 	 *                                                  'description', 'parent', 'term_order'). Unless `$object_ids`
   102 	 *                                                  is not empty, 'term_order' is treated the same as 'term_id'.
   102 	 *                                                  is not empty, 'term_order' is treated the same as 'term_id'.
   103 	 *                                                - 'count' for term taxonomy count.
   103 	 *                                                * 'count' to use the number of objects associated with the term.
   104 	 *                                                - 'include' to match the 'order' of the $include param.
   104 	 *                                                * 'include' to match the 'order' of the $include param.
   105 	 *                                                - 'slug__in' to match the 'order' of the $slug param.
   105 	 *                                                * 'slug__in' to match the 'order' of the $slug param.
   106 	 *                                                - 'meta_value', 'meta_value_num'.
   106 	 *                                                * 'meta_value', 'meta_value_num'.
   107 	 *                                                - the value of `$meta_key`.
   107 	 *                                                  the value of `$meta_key`.
   108 	 *                                                - the array keys of `$meta_query`.
   108 	 *                                                  the array keys of `$meta_query`.
   109 	 *                                                - 'none' to omit the ORDER BY clause.
   109 	 *                                                * 'none' to omit the ORDER BY clause.
   110 	 *                                                Defaults to 'name'.
   110 	 *                                                Defaults to 'name'.
   111 	 *     @type string       $order                  Whether to order terms in ascending or descending order.
   111 	 *     @type string       $order                  Whether to order terms in ascending or descending order.
   112 	 *                                                Accepts 'ASC' (ascending) or 'DESC' (descending).
   112 	 *                                                Accepts 'ASC' (ascending) or 'DESC' (descending).
   113 	 *                                                Default 'ASC'.
   113 	 *                                                Default 'ASC'.
   114 	 *     @type bool|int     $hide_empty             Whether to hide terms not assigned to any posts. Accepts
   114 	 *     @type bool|int     $hide_empty             Whether to hide terms not assigned to any posts. Accepts
   115 	 *                                                1|true or 0|false. Default 1|true.
   115 	 *                                                1|true or 0|false. Default 1|true.
   116 	 *     @type array|string $include                Array or comma/space-separated string of term IDs to include.
   116 	 *     @type int[]|string $include                Array or comma/space-separated string of term IDs to include.
   117 	 *                                                Default empty array.
   117 	 *                                                Default empty array.
   118 	 *     @type array|string $exclude                Array or comma/space-separated string of term IDs to exclude.
   118 	 *     @type int[]|string $exclude                Array or comma/space-separated string of term IDs to exclude.
   119 	 *                                                If $include is non-empty, $exclude is ignored.
   119 	 *                                                If $include is non-empty, $exclude is ignored.
   120 	 *                                                Default empty array.
   120 	 *                                                Default empty array.
   121 	 *     @type array|string $exclude_tree           Array or comma/space-separated string of term IDs to exclude
   121 	 *     @type int[]|string $exclude_tree           Array or comma/space-separated string of term IDs to exclude
   122 	 *                                                along with all of their descendant terms. If $include is
   122 	 *                                                along with all of their descendant terms. If $include is
   123 	 *                                                non-empty, $exclude_tree is ignored. Default empty array.
   123 	 *                                                non-empty, $exclude_tree is ignored. Default empty array.
   124 	 *     @type int|string   $number                 Maximum number of terms to return. Accepts ''|0 (all) or any
   124 	 *     @type int|string   $number                 Maximum number of terms to return. Accepts ''|0 (all) or any
   125 	 *                                                positive number. Default ''|0 (all). Note that $number may
   125 	 *                                                positive number. Default ''|0 (all). Note that $number may
   126 	 *                                                not return accurate results when coupled with $object_ids.
   126 	 *                                                not return accurate results when coupled with $object_ids.
   127 	 *                                                See #41796 for details.
   127 	 *                                                See #41796 for details.
   128 	 *     @type int          $offset                 The number by which to offset the terms query. Default empty.
   128 	 *     @type int          $offset                 The number by which to offset the terms query. Default empty.
   129 	 *     @type string       $fields                 Term fields to query for. Accepts:
   129 	 *     @type string       $fields                 Term fields to query for. Accepts:
   130 	 *                                                - 'all' Returns an array of complete term objects (`WP_Term[]`).
   130 	 *                                                * 'all' Returns an array of complete term objects (`WP_Term[]`).
   131 	 *                                                - 'all_with_object_id' Returns an array of term objects
   131 	 *                                                * 'all_with_object_id' Returns an array of term objects
   132 	 *                                                  with the 'object_id' param (`WP_Term[]`). Works only
   132 	 *                                                  with the 'object_id' param (`WP_Term[]`). Works only
   133 	 *                                                  when the `$object_ids` parameter is populated.
   133 	 *                                                  when the `$object_ids` parameter is populated.
   134 	 *                                                - 'ids' Returns an array of term IDs (`int[]`).
   134 	 *                                                * 'ids' Returns an array of term IDs (`int[]`).
   135 	 *                                                - 'tt_ids' Returns an array of term taxonomy IDs (`int[]`).
   135 	 *                                                * 'tt_ids' Returns an array of term taxonomy IDs (`int[]`).
   136 	 *                                                - 'names' Returns an array of term names (`string[]`).
   136 	 *                                                * 'names' Returns an array of term names (`string[]`).
   137 	 *                                                - 'slugs' Returns an array of term slugs (`string[]`).
   137 	 *                                                * 'slugs' Returns an array of term slugs (`string[]`).
   138 	 *                                                - 'count' Returns the number of matching terms (`int`).
   138 	 *                                                * 'count' Returns the number of matching terms (`int`).
   139 	 *                                                - 'id=>parent' Returns an associative array of parent term IDs,
   139 	 *                                                * 'id=>parent' Returns an associative array of parent term IDs,
   140 	 *                                                   keyed by term ID (`int[]`).
   140 	 *                                                   keyed by term ID (`int[]`).
   141 	 *                                                - 'id=>name' Returns an associative array of term names,
   141 	 *                                                * 'id=>name' Returns an associative array of term names,
   142 	 *                                                   keyed by term ID (`string[]`).
   142 	 *                                                   keyed by term ID (`string[]`).
   143 	 *                                                - 'id=>slug' Returns an associative array of term slugs,
   143 	 *                                                * 'id=>slug' Returns an associative array of term slugs,
   144 	 *                                                   keyed by term ID (`string[]`).
   144 	 *                                                   keyed by term ID (`string[]`).
   145 	 *                                                Default 'all'.
   145 	 *                                                Default 'all'.
   146 	 *     @type bool         $count                  Whether to return a term count. If true, will take precedence
   146 	 *     @type bool         $count                  Whether to return a term count. If true, will take precedence
   147 	 *                                                over `$fields`. Default false.
   147 	 *                                                over `$fields`. Default false.
   148 	 *     @type string|array $name                   Optional. Name or array of names to return term(s) for.
   148 	 *     @type string|array $name                   Optional. Name or array of names to return term(s) for.
   149 	 *                                                Default empty.
   149 	 *                                                Default empty.
   150 	 *     @type string|array $slug                   Optional. Slug or array of slugs to return term(s) for.
   150 	 *     @type string|array $slug                   Optional. Slug or array of slugs to return term(s) for.
   151 	 *                                                Default empty.
   151 	 *                                                Default empty.
   152 	 *     @type int|array    $term_taxonomy_id       Optional. Term taxonomy ID, or array of term taxonomy IDs,
   152 	 *     @type int|int[]    $term_taxonomy_id       Optional. Term taxonomy ID, or array of term taxonomy IDs,
   153 	 *                                                to match when querying terms.
   153 	 *                                                to match when querying terms.
   154 	 *     @type bool         $hierarchical           Whether to include terms that have non-empty descendants
   154 	 *     @type bool         $hierarchical           Whether to include terms that have non-empty descendants
   155 	 *                                                (even if $hide_empty is set to true). Default true.
   155 	 *                                                (even if $hide_empty is set to true). Default true.
   156 	 *     @type string       $search                 Search criteria to match terms. Will be SQL-formatted with
   156 	 *     @type string       $search                 Search criteria to match terms. Will be SQL-formatted with
   157 	 *                                                wildcards before and after. Default empty.
   157 	 *                                                wildcards before and after. Default empty.
   256 
   256 
   257 		$query['number'] = absint( $query['number'] );
   257 		$query['number'] = absint( $query['number'] );
   258 		$query['offset'] = absint( $query['offset'] );
   258 		$query['offset'] = absint( $query['offset'] );
   259 
   259 
   260 		// 'parent' overrides 'child_of'.
   260 		// 'parent' overrides 'child_of'.
   261 		if ( 0 < intval( $query['parent'] ) ) {
   261 		if ( 0 < (int) $query['parent'] ) {
   262 			$query['child_of'] = false;
   262 			$query['child_of'] = false;
   263 		}
   263 		}
   264 
   264 
   265 		if ( 'all' === $query['get'] ) {
   265 		if ( 'all' === $query['get'] ) {
   266 			$query['childless']    = false;
   266 			$query['childless']    = false;
   283 		 */
   283 		 */
   284 		do_action( 'parse_term_query', $this );
   284 		do_action( 'parse_term_query', $this );
   285 	}
   285 	}
   286 
   286 
   287 	/**
   287 	/**
   288 	 * Sets up the query for retrieving terms.
   288 	 * Sets up the query and retrieves the results.
       
   289 	 *
       
   290 	 * The return type varies depending on the value passed to `$args['fields']`. See
       
   291 	 * WP_Term_Query::get_terms() for details.
   289 	 *
   292 	 *
   290 	 * @since 4.6.0
   293 	 * @since 4.6.0
   291 	 *
   294 	 *
   292 	 * @param string|array $query Array or URL query string of parameters.
   295 	 * @param string|array $query Array or URL query string of parameters.
   293 	 * @return array|int List of terms, or number of terms when 'count' is passed as a query var.
   296 	 * @return WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string
       
   297 	 *                                         when 'count' is passed as a query var.
   294 	 */
   298 	 */
   295 	public function query( $query ) {
   299 	public function query( $query ) {
   296 		$this->query_vars = wp_parse_args( $query );
   300 		$this->query_vars = wp_parse_args( $query );
   297 		return $this->get_terms();
   301 		return $this->get_terms();
   298 	}
   302 	}
   299 
   303 
   300 	/**
   304 	/**
   301 	 * Get terms, based on query_vars.
   305 	 * Retrieves the query results.
       
   306 	 *
       
   307 	 * The return type varies depending on the value passed to `$args['fields']`.
       
   308 	 *
       
   309 	 * The following will result in an array of `WP_Term` objects being returned:
       
   310 	 *
       
   311 	 *   - 'all'
       
   312 	 *   - 'all_with_object_id'
       
   313 	 *
       
   314 	 * The following will result in a numeric string being returned:
       
   315 	 *
       
   316 	 *   - 'count'
       
   317 	 *
       
   318 	 * The following will result in an array of text strings being returned:
       
   319 	 *
       
   320 	 *   - 'id=>name'
       
   321 	 *   - 'id=>slug'
       
   322 	 *   - 'names'
       
   323 	 *   - 'slugs'
       
   324 	 *
       
   325 	 * The following will result in an array of numeric strings being returned:
       
   326 	 *
       
   327 	 *   - 'id=>parent'
       
   328 	 *
       
   329 	 * The following will result in an array of integers being returned:
       
   330 	 *
       
   331 	 *   - 'ids'
       
   332 	 *   - 'tt_ids'
   302 	 *
   333 	 *
   303 	 * @since 4.6.0
   334 	 * @since 4.6.0
   304 	 *
   335 	 *
   305 	 * @global wpdb $wpdb WordPress database abstraction object.
   336 	 * @global wpdb $wpdb WordPress database abstraction object.
   306 	 *
   337 	 *
   307 	 * @return array List of terms.
   338 	 * @return WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string
       
   339 	 *                                         when 'count' is passed as a query var.
   308 	 */
   340 	 */
   309 	public function get_terms() {
   341 	public function get_terms() {
   310 		global $wpdb;
   342 		global $wpdb;
   311 
   343 
   312 		$this->parse_query( $this->query_vars );
   344 		$this->parse_query( $this->query_vars );
   319 		/**
   351 		/**
   320 		 * Fires before terms are retrieved.
   352 		 * Fires before terms are retrieved.
   321 		 *
   353 		 *
   322 		 * @since 4.6.0
   354 		 * @since 4.6.0
   323 		 *
   355 		 *
   324 		 * @param WP_Term_Query $this Current instance of WP_Term_Query.
   356 		 * @param WP_Term_Query $this Current instance of WP_Term_Query (passed by reference).
   325 		 */
   357 		 */
   326 		do_action( 'pre_get_terms', $this );
   358 		do_action_ref_array( 'pre_get_terms', array( &$this ) );
   327 
   359 
   328 		$taxonomies = (array) $args['taxonomy'];
   360 		$taxonomies = (array) $args['taxonomy'];
   329 
   361 
   330 		// Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
   362 		// Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
   331 		$has_hierarchical_tax = false;
   363 		$has_hierarchical_tax = false;
   344 			$args['hierarchical'] = false;
   376 			$args['hierarchical'] = false;
   345 			$args['pad_counts']   = false;
   377 			$args['pad_counts']   = false;
   346 		}
   378 		}
   347 
   379 
   348 		// 'parent' overrides 'child_of'.
   380 		// 'parent' overrides 'child_of'.
   349 		if ( 0 < intval( $args['parent'] ) ) {
   381 		if ( 0 < (int) $args['parent'] ) {
   350 			$args['child_of'] = false;
   382 			$args['child_of'] = false;
   351 		}
   383 		}
   352 
   384 
   353 		if ( 'all' === $args['get'] ) {
   385 		if ( 'all' === $args['get'] ) {
   354 			$args['childless']    = false;
   386 			$args['childless']    = false;
   441 				$excluded_children = array_merge(
   473 				$excluded_children = array_merge(
   442 					$excluded_children,
   474 					$excluded_children,
   443 					(array) get_terms(
   475 					(array) get_terms(
   444 						array(
   476 						array(
   445 							'taxonomy'   => reset( $taxonomies ),
   477 							'taxonomy'   => reset( $taxonomies ),
   446 							'child_of'   => intval( $extrunk ),
   478 							'child_of'   => (int) $extrunk,
   447 							'fields'     => 'ids',
   479 							'fields'     => 'ids',
   448 							'hide_empty' => 0,
   480 							'hide_empty' => 0,
   449 						)
   481 						)
   450 					)
   482 					)
   451 				);
   483 				);
   686 		$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
   718 		$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
   687 
   719 
   688 		$this->terms = null;
   720 		$this->terms = null;
   689 
   721 
   690 		/**
   722 		/**
   691 		 * Filter the terms array before the query takes place.
   723 		 * Filters the terms array before the query takes place.
   692 		 *
   724 		 *
   693 		 * Return a non-null value to bypass WordPress's default term queries.
   725 		 * Return a non-null value to bypass WordPress' default term queries.
   694 		 *
   726 		 *
   695 		 * @since 5.3.0
   727 		 * @since 5.3.0
   696 		 *
   728 		 *
   697 		 * @param array|null    $terms Return an array of term data to short-circuit WP's term query,
   729 		 * @param array|null    $terms Return an array of term data to short-circuit WP's term query,
   698 		 *                             or null to allow WP queries to run normally.
   730 		 *                             or null to allow WP queries to run normally.
   699 		 * @param WP_Term_Query $this  The WP_Term_Query instance, passed by reference.
   731 		 * @param WP_Term_Query $query The WP_Term_Query instance, passed by reference.
   700 		 */
   732 		 */
   701 		$this->terms = apply_filters_ref_array( 'terms_pre_query', array( $this->terms, &$this ) );
   733 		$this->terms = apply_filters_ref_array( 'terms_pre_query', array( $this->terms, &$this ) );
   702 
   734 
   703 		if ( null !== $this->terms ) {
   735 		if ( null !== $this->terms ) {
   704 			return $this->terms;
   736 			return $this->terms;