WP_Term_Query::get_terms() – Retrieves the query results.

You appear to be a bot. Output may be restricted

Description

Retrieves the query results.

The return type varies depending on the value passed to `$args['fields']`. The following will result in an array of WP_Term objects being returned:

  • 'all'
  • 'all_with_object_id'

The following will result in a numeric string being returned:

  • 'count'

The following will result in an array of text strings being returned:

  • 'id=>name'
  • 'id=>slug'
  • 'names'
  • 'slugs'

The following will result in an array of numeric strings being returned:

  • 'id=>parent'

The following will result in an array of integers being returned:

  • 'ids'
  • 'tt_ids'

Usage

$WP_Term[]|int[]|string[]|string = WP_Term_Query::get_terms();

Parameters

Returns

WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string when 'count' is passed as a query var.

Source

File name: wordpress/wp-includes/class-wp-term-query.php


Lines:

1 to 100 of 554
  public function get_terms() {
    global $wpdb;

    $this->WP_Term_Query::parse_query( $this->query_vars );
    $args = &$this->query_vars;

    // Set up meta_query so it's available to 'pre_get_terms'.
    $this->meta_query = new WP_Meta_Query();
    $this->meta_query->parse_query_vars( $args );

    
/**
 * Fires before terms are retrieved.
 *
 * @since 4.6.0
 *
 * @param WP_Term_Query $query Current instance of WP_Term_Query (passed by reference).
 */
    do_action_ref_array( 'pre_get_terms', array( &$this ) );

    $taxonomies = (array) $args['taxonomy'];

    // Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
    $has_hierarchical_tax = false;
    if ( $taxonomies ) {
      foreach ( $taxonomies as $_tax ) {
        if ( is_taxonomy_hierarchical( $_tax ) ) {
          $has_hierarchical_tax = true;
        }
      }
    } else {
      // When no taxonomies are provided, assume we have to descend the tree.
      $has_hierarchical_tax = true;
    }

    if ( ! $has_hierarchical_tax ) {
      $args['hierarchical'] = false;
      $args['pad_counts']   = false;
    }

    // 'parent' overrides 'child_of'.
    if ( 0 < (int) $args['parent'] ) {
      $args['child_of'] = false;
    }

    if ( 'all' === $args['get'] ) {
      $args['childless']    = false;
      $args['child_of']     = 0;
      $args['hide_empty']   = 0;
      $args['hierarchical'] = false;
      $args['pad_counts']   = false;
    }

    
/**
 * Filters the terms query arguments.
 *
 * @since 3.1.0
 *
 * @param array    $args       An array of get_terms() arguments.
 * @param string[] $taxonomies An array of taxonomy names.
 */
    $args = apply_filters( 'get_terms_args', $args, $taxonomies );

    // Avoid the query if the queried parent/child_of term has no descendants.
    $child_of = $args['child_of'];
    $parent   = $args['parent'];

    if ( $child_of ) {
      $_parent = $child_of;
    } elseif ( $parent ) {
      $_parent = $parent;
    } else {
      $_parent = false;
    }

    if ( $_parent ) {
      $in_hierarchy = false;
      foreach ( $taxonomies as $_tax ) {
        $hierarchy = _get_term_hierarchy( $_tax );

        if ( isset( $hierarchy[ $_parent ] ) ) {
          $in_hierarchy = true;
        }
      }

      if ( ! $in_hierarchy ) {
        if ( 'count' === $args['fields'] ) {
          return 0;
        } else {
          $this->terms = array();
          return $this->terms;
        }
      }
    }

    // 'term_order' is a legal sort order only when joining the relationship table.
    $_orderby = $this->query_vars['orderby'];
    if ( 'term_order' === $_orderby && empty( $this->query_vars['object_ids'] ) ) {
      $_orderby = 'term_id';

 View on GitHub View on Trac