wppaste
WordPress

get_term_parents_list( int $term_id, string $taxonomy, string|array $args = array() ): string|WP_Error

Since
4.8.0
Source
wp-includes/category-template.php:1398
Retrieves term parents with separator.

Compatibility

WordPress
since 4.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$term_idint
Term ID.
$taxonomystring
Taxonomy name.
$argsstring|arrayoptional
Array of optional arguments.Default: array()
  • $formatstringdefault: 'name'

    Use term names or slugs for display. Accepts 'name' or 'slug'.
  • $separatorstringdefault: '/'

    Separator for between the terms.
  • $linkbooldefault: true

    Whether to format as a link.
  • $inclusivebooldefault: true

    Include the term to get the parents for.

Return value

string|WP_Error
A list of term parents on success, WP_Error or empty string on failure.

Performance profile

How much work a call to get_term_parents_list() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_term().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–44

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 89.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_term()called directly
  • hookthird-party callbacksapply_filters()one call below get_term_parents_list()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_term_parents_list() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14–15get_term(), is_wp_error()
!is_wp_error() && !$args38–40get_term(), is_wp_error(), wp_parse_args(), get_ancestors(), array_reverse()
!is_wp_error() && $args42–44get_term(), is_wp_error(), wp_parse_args(), get_ancestors(), array_unshift(), array_reverse()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8914–449
8.58914–449
8.48914–449
8.38914–449
8.28914–449
8.18914–4492 fewer instructions than PHP 7.4
7.49114–449

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 1

Source code

function get_term_parents_list( $term_id, $taxonomy, $args = array() ) {	$list = '';	$term = get_term( $term_id, $taxonomy ); 	if ( is_wp_error( $term ) ) {		return $term;	} 	if ( ! $term ) {		return $list;	} 	$term_id = $term->term_id; 	$defaults = array(		'format'    => 'name',		'separator' => '/',		'link'      => true,		'inclusive' => true,	); 	$args = wp_parse_args( $args, $defaults ); 	foreach ( array( 'link', 'inclusive' ) as $bool ) {		$args[ $bool ] = wp_validate_boolean( $args[ $bool ] );	} 	$parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' ); 	if ( $args['inclusive'] ) {		array_unshift( $parents, $term_id );	} 	foreach ( array_reverse( $parents ) as $term_id ) {		$parent = get_term( $term_id, $taxonomy );		$name   = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name; 		if ( $args['link'] ) {			$list .= '<a href="' . esc_url( get_term_link( $parent->term_id, $taxonomy ) ) . '">' . $name . '</a>' . $args['separator'];		} else {			$list .= $name . $args['separator'];		}	} 	return $list;}

Changelog

Introduced in 4.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/category-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.