: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$exclusions = array_merge( $excluded_children, $exclusions );
if ( ! empty( $exclude ) ) {
$exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions );
// 'childless' terms are those without an entry in the flattened term hierarchy.
$childless = (bool) $args['childless'];
foreach ( $taxonomies as $_tax ) {
$term_hierarchy = _get_term_hierarchy( $_tax );
$exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions );
if ( ! empty( $exclusions ) ) {
$exclusions = 't.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')';
* Filters the terms to exclude from the terms query.
* @param string $exclusions `NOT IN` clause of the terms query.
* @param array $args An array of terms query arguments.
* @param string[] $taxonomies An array of taxonomy names.
$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
if ( ! empty( $exclusions ) ) {
// Strip leading 'AND'. Must do string manipulation here for backward compatibility with filter.
$this->sql_clauses['where']['exclusions'] = preg_replace( '/^\s*AND\s*/', '', $exclusions );
if ( '' === $args['name'] ) {
$args['name'] = (array) $args['name'];
if ( ! empty( $args['name'] ) ) {
foreach ( $names as &$_name ) {
// `sanitize_term_field()` returns slashed data.
$_name = stripslashes( sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' ) );
$this->sql_clauses['where']['name'] = "t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')";
if ( '' === $args['slug'] ) {
$args['slug'] = array_map( 'sanitize_title', (array) $args['slug'] );
if ( ! empty( $args['slug'] ) ) {
$slug = implode( "', '", $args['slug'] );
$this->sql_clauses['where']['slug'] = "t.slug IN ('" . $slug . "')";
if ( '' === $args['term_taxonomy_id'] ) {
$args['term_taxonomy_id'] = array();
$args['term_taxonomy_id'] = array_map( 'intval', (array) $args['term_taxonomy_id'] );
if ( ! empty( $args['term_taxonomy_id'] ) ) {
$tt_ids = implode( ',', $args['term_taxonomy_id'] );
$this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
if ( ! empty( $args['name__like'] ) ) {
$this->sql_clauses['where']['name__like'] = $wpdb->prepare(
'%' . $wpdb->esc_like( $args['name__like'] ) . '%'
if ( ! empty( $args['description__like'] ) ) {
$this->sql_clauses['where']['description__like'] = $wpdb->prepare(
'tt.description LIKE %s',
'%' . $wpdb->esc_like( $args['description__like'] ) . '%'
if ( '' === $args['object_ids'] ) {
$args['object_ids'] = array();
$args['object_ids'] = array_map( 'intval', (array) $args['object_ids'] );
if ( ! empty( $args['object_ids'] ) ) {
$object_ids = implode( ', ', $args['object_ids'] );
$this->sql_clauses['where']['object_ids'] = "tr.object_id IN ($object_ids)";
* When querying for object relationships, the 'count > 0' check
* added by 'hide_empty' is superfluous.
if ( ! empty( $args['object_ids'] ) ) {
$args['hide_empty'] = false;
$this->sql_clauses['where']['parent'] = "tt.parent = '$parent'";
$hierarchical = $args['hierarchical'];
if ( 'count' === $args['fields'] ) {
if ( $args['hide_empty'] && ! $hierarchical ) {
$this->sql_clauses['where']['count'] = 'tt.count > 0';
$number = $args['number'];
$offset = $args['offset'];
// Don't limit the query results when we have to descend the family tree.
if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
$limits = 'LIMIT ' . $offset . ',' . $number;
$limits = 'LIMIT ' . $number;
if ( ! empty( $args['search'] ) ) {
$this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] );
// Reparse meta_query query_vars, in case they were modified in a 'pre_get_terms' callback.
$this->meta_query->parse_query_vars( $this->query_vars );
$mq_sql = $this->meta_query->get_sql( 'term', 't', 'term_id' );
$meta_clauses = $this->meta_query->get_clauses();
if ( ! empty( $meta_clauses ) ) {
$join .= $mq_sql['join'];
$this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] );
switch ( $args['fields'] ) {
$selects = array( 'COUNT(*)' );
$selects = array( 't.term_id' );
if ( 'all_with_object_id' === $args['fields'] && ! empty( $args['object_ids'] ) ) {
$selects[] = 'tr.object_id';
$_fields = $args['fields'];
* Filters the fields to select in the terms query.
* Field lists modified using this filter will only modify the term fields returned
* by the function when the `$fields` parameter set to 'count' or 'all'. In all other
* cases, the term fields in the results array will be determined by the `$fields`
* Use of this filter can result in unpredictable behavior, and is not recommended.
* @param string[] $selects An array of fields to select for the terms query.
* @param array $args An array of term query arguments.
* @param string[] $taxonomies An array of taxonomy names.
$fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
$join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
if ( ! empty( $this->query_vars['object_ids'] ) ) {
$join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id";
$where = implode( ' AND ', $this->sql_clauses['where'] );
$pieces = array( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );
* Filters the terms query SQL clauses.
* @param string[] $clauses {
* Associative array of the clauses for the query.
* @type string $fields The SELECT clause of the query.
* @type string $join The JOIN clause of the query.
* @type string $where The WHERE clause of the query.
* @type string $distinct The DISTINCT clause of the query.
* @type string $orderby The ORDER BY clause of the query.
* @type string $order The ORDER clause of the query.
* @type string $limits The LIMIT clause of the query.
* @param string[] $taxonomies An array of taxonomy names.
* @param array $args An array of term query arguments.
$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
$fields = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
$join = isset( $clauses['join'] ) ? $clauses['join'] : '';
$where = isset( $clauses['where'] ) ? $clauses['where'] : '';
$distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : '';
$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
$order = isset( $clauses['order'] ) ? $clauses['order'] : '';
$limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
$fields_is_filtered = implode( ', ', $selects ) !== $fields;
$this->sql_clauses['select'] = "SELECT $distinct $fields";
$this->sql_clauses['from'] = "FROM $wpdb->terms AS t $join";
$this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : '';
$this->sql_clauses['limits'] = $limits;
// Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
"{$this->sql_clauses['select']}
{$this->sql_clauses['from']}
{$this->sql_clauses['orderby']}
{$this->sql_clauses['limits']}";
* Filters the terms array before the query takes place.
* Return a non-null value to bypass WordPress' default term queries.
* @param array|null $terms Return an array of term data to short-circuit WP's term query,
* or null to allow WP queries to run normally.
* @param WP_Term_Query $query The WP_Term_Query instance, passed by reference.
$this->terms = apply_filters_ref_array( 'terms_pre_query', array( $this->terms, &$this ) );
if ( null !== $this->terms ) {
if ( $args['cache_results'] ) {
$cache_key = $this->generate_cache_key( $args, $this->request );
$cache = wp_cache_get( $cache_key, 'term-queries' );
if ( false !== $cache ) {
if ( 'ids' === $_fields ) {
$cache = array_map( 'intval', $cache );
} elseif ( 'count' !== $_fields ) {
if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
|| ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered )
$term_ids = wp_list_pluck( $cache, 'term_id' );
$term_ids = array_map( 'intval', $cache );
_prime_term_caches( $term_ids, $args['update_term_meta_cache'] );
$term_objects = $this->populate_terms( $cache );
$cache = $this->format_terms( $term_objects, $_fields );
if ( 'count' === $_fields ) {
$count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( $args['cache_results'] ) {
wp_cache_set( $cache_key, $count, 'term-queries' );
$terms = $wpdb->get_results( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( $args['cache_results'] ) {
wp_cache_add( $cache_key, array(), 'term-queries' );
$term_ids = wp_list_pluck( $terms, 'term_id' );
_prime_term_caches( $term_ids, false );
$term_objects = $this->populate_terms( $terms );
foreach ( $taxonomies as $_tax ) {
$children = _get_term_hierarchy( $_tax );
if ( ! empty( $children ) ) {
$term_objects = _get_term_children( $child_of, $term_objects, $_tax );
// Update term counts to include children.
if ( $args['pad_counts'] && 'all' === $_fields ) {
foreach ( $taxonomies as $_tax ) {
_pad_term_counts( $term_objects, $_tax );
// Make sure we show empty categories that have children.
if ( $hierarchical && $args['hide_empty'] && is_array( $term_objects ) ) {
foreach ( $term_objects as $k => $term ) {
$children = get_term_children( $term->term_id, $term->taxonomy );
if ( is_array( $children ) ) {
foreach ( $children as $child_id ) {
$child = get_term( $child_id, $term->taxonomy );
unset( $term_objects[ $k ] );
// Hierarchical queries are not limited, so 'offset' and 'number' must be handled now.
if ( $hierarchical && $number && is_array( $term_objects ) ) {
if ( $offset >= count( $term_objects ) ) {
$term_objects = array_slice( $term_objects, $offset, $number, true );
if ( $args['update_term_meta_cache'] ) {
$term_ids = wp_list_pluck( $term_objects, 'term_id' );
wp_lazyload_term_meta( $term_ids );
if ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) ) {
foreach ( $term_objects as $term ) {
$object = new stdClass();
$object->term_id = $term->term_id;
$object->object_id = $term->object_id;
} elseif ( 'all' === $_fields && $args['pad_counts'] ) {
foreach ( $term_objects as $term ) {
$object = new stdClass();
$object->term_id = $term->term_id;
$object->count = $term->count;
} elseif ( $fields_is_filtered ) {
$term_cache = $term_objects;
$term_cache = wp_list_pluck( $term_objects, 'term_id' );
if ( $args['cache_results'] ) {
wp_cache_add( $cache_key, $term_cache, 'term-queries' );
$this->terms = $this->format_terms( $term_objects, $_fields );
* Parse and sanitize 'orderby' keys passed to the term query.
* @param string $orderby_raw Alias for the field to order by.
* @return string|false Value to used in the ORDER clause. False otherwise.
protected function parse_orderby( $orderby_raw ) {
$_orderby = strtolower( $orderby_raw );
$maybe_orderby_meta = false;
if ( in_array( $_orderby, array( 'term_id', 'name', 'slug', 'term_group' ), true ) ) {
$orderby = "t.$_orderby";
} elseif ( in_array( $_orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id', 'description' ), true ) ) {
$orderby = "tt.$_orderby";
} elseif ( 'term_order' === $_orderby ) {
$orderby = 'tr.term_order';
} elseif ( 'include' === $_orderby && ! empty( $this->query_vars['include'] ) ) {
$include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) );
$orderby = "FIELD( t.term_id, $include )";
} elseif ( 'slug__in' === $_orderby && ! empty( $this->query_vars['slug'] ) && is_array( $this->query_vars['slug'] ) ) {
$slugs = implode( "', '", array_map( 'sanitize_title_for_query', $this->query_vars['slug'] ) );
$orderby = "FIELD( t.slug, '" . $slugs . "')";
} elseif ( 'none' === $_orderby ) {
} elseif ( empty( $_orderby ) || 'id' === $_orderby || 'term_id' === $_orderby ) {
// This may be a value of orderby related to meta.
$maybe_orderby_meta = true;
* Filters the ORDERBY clause of the terms query.
* @param string $orderby `ORDERBY` clause of the terms query.
* @param array $args An array of term query arguments.
* @param string[] $taxonomies An array of taxonomy names.
$orderby = apply_filters( 'get_terms_orderby', $orderby, $this->query_vars, $this->query_vars['taxonomy'] );
// Run after the 'get_terms_orderby' filter for backward compatibility.
if ( $maybe_orderby_meta ) {
$maybe_orderby_meta = $this->parse_orderby_meta( $_orderby );
if ( $maybe_orderby_meta ) {
$orderby = $maybe_orderby_meta;
* Format response depending on field requested.
* @param WP_Term[] $term_objects Array of term objects.
* @param string $_fields Field to format.
* @return WP_Term[]|int[]|string[] Array of terms / strings / ints depending on field requested.
protected function format_terms( $term_objects, $_fields ) {
if ( 'id=>parent' === $_fields ) {
foreach ( $term_objects as $term ) {
$_terms[ $term->term_id ] = $term->parent;
} elseif ( 'ids' === $_fields ) {
foreach ( $term_objects as $term ) {
$_terms[] = (int) $term->term_id;
} elseif ( 'tt_ids' === $_fields ) {
foreach ( $term_objects as $term ) {
$_terms[] = (int) $term->term_taxonomy_id;
} elseif ( 'names' === $_fields ) {
foreach ( $term_objects as $term ) {
} elseif ( 'slugs' === $_fields ) {
foreach ( $term_objects as $term ) {