wppaste
WordPress

get_term_link( WP_Term|int|string $term, string $taxonomy = '' ): string|WP_Error

Since
2.5.0
Source
wp-includes/taxonomy.php:4633
Generates a permalink for a taxonomy term archive.

Compatibility

WordPress
since 2.5.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

$termWP_Term|int|string
The term object, ID, or slug whose link will be retrieved.
$taxonomystringoptional
Taxonomy. Default empty.Default: ''

Return value

string|WP_Error
URL of the taxonomy term archive on success, WP_Error if term does not exist.

Performance profile

How much work a call to get_term_link() 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
12–103

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

Plugin surface
4 hooks

Third-party callbacks on 'pre_term_link', 'tag_link', 'category_link' run inside this call, and their cost is not bounded by anything here.

Called by
18

18 places 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()called directly

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 · 42 distinct outcomes

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

WhenInstructionsCalls it makes
is_object($term) && is_wp_error()12is_wp_error()
is_wp_error()20__(), is_wp_error()
!is_int($term) && is_wp_error()20get_term_by(), is_wp_error()
is_int($term) && is_wp_error()20get_term(), is_wp_error()
!is_object($term) && !is_int($term) && is_wp_error()28get_term_by(), __(), is_wp_error()
!is_object($term) && is_int($term) && is_wp_error()28get_term(), __(), is_wp_error()
is_object($term) && !is_wp_error() && empty($termlink) && $taxonomy !== "category" && $taxonomy !== "post_tag"52–57is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters()
is_object($term) && !is_wp_error() && !empty($termlink) && empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"58is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters()
is_object($term) && !is_wp_error() && empty($termlink)59–65is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters(), apply_filters()
!is_wp_error() && empty($termlink) && $taxonomy !== "category" && $taxonomy !== "post_tag"60–65__(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters()
!is_int($term) && !is_wp_error() && empty($termlink) && $taxonomy !== "category" && $taxonomy !== "post_tag"60–65get_term_by(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters()
is_int($term) && !is_wp_error() && empty($termlink) && $taxonomy !== "category" && $taxonomy !== "post_tag"60–65get_term(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters()
30 further outcomes, up to 103 instructions
is_object($term) && !is_wp_error() && !empty($termlink) && empty($value)65–66is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_wp_error() && !empty($termlink) && empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"66__(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters()
!is_int($term) && !is_wp_error() && !empty($termlink) && empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"66get_term_by(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters()
is_int($term) && !is_wp_error() && !empty($termlink) && empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"66get_term(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters()
!is_wp_error() && empty($termlink)67–73__(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters(), apply_filters()
!is_int($term) && !is_wp_error() && empty($termlink)67–73get_term_by(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters(), apply_filters()
is_int($term) && !is_wp_error() && empty($termlink)67–73get_term(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters(), apply_filters()
!is_object($term) && !is_int($term) && !is_wp_error() && empty($termlink) && $taxonomy !== "category" && $taxonomy !== "post_tag"68–73get_term_by(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters()
!is_object($term) && is_int($term) && !is_wp_error() && empty($termlink) && $taxonomy !== "category" && $taxonomy !== "post_tag"68–73get_term(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters()
!is_wp_error() && !empty($termlink) && empty($value)73–74__(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_int($term) && !is_wp_error() && !empty($termlink) && empty($value)73–74get_term_by(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
is_int($term) && !is_wp_error() && !empty($termlink) && empty($value)73–74get_term(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_object($term) && !is_int($term) && !is_wp_error() && !empty($termlink) && empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"74get_term_by(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters()
!is_object($term) && is_int($term) && !is_wp_error() && !empty($termlink) && empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"74get_term(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters()
!is_object($term) && !is_int($term) && !is_wp_error() && empty($termlink)75–81get_term_by(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters(), apply_filters()
!is_object($term) && is_int($term) && !is_wp_error() && empty($termlink)75–81get_term(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), home_url(), apply_filters(), apply_filters()
is_object($term) && !is_wp_error() && !empty($termlink) && !empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"78–79is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters()
!is_object($term) && !is_int($term) && !is_wp_error() && !empty($termlink) && empty($value)81–82get_term_by(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_object($term) && is_int($term) && !is_wp_error() && !empty($termlink) && empty($value)81–82get_term(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
is_object($term) && !is_wp_error() && !empty($termlink) && !empty($value)85–87is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_wp_error() && !empty($termlink) && !empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"86–87__(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters()
!is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"86–87get_term_by(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters()
is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"86–87get_term(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters()
!is_wp_error() && !empty($termlink) && !empty($value)93–95__(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value)93–95get_term_by(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value)93–95get_term(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_object($term) && !is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"94–95get_term_by(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters()
!is_object($term) && is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value) && $taxonomy !== "post_tag" && $taxonomy !== "category"94–95get_term(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters()
!is_object($term) && !is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value)101–103get_term_by(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()
!is_object($term) && is_int($term) && !is_wp_error() && !empty($termlink) && !empty($value)101–103get_term(), __(), is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), get_ancestors(), array_reverse(), user_trailingslashit(), home_url(), apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15912–10312
8.515912–10312
8.415912–103129 fewer instructions than PHP 8.3
8.316812–10912
8.216812–10912
8.116812–10912
7.416812–10912

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.

Hooks and filters fired · 4

4 hooks fire while get_term_link() runs, in this order:

  1. apply_filters( pre_term_link )filterline 4664 (+31 into the body)

    Filters the permalink structure for a term before token replacement occurs.

  2. apply_filters( tag_link )filterline 4708 (+75 into the body)

    Filters the tag link.

  3. apply_filters( category_link )filterline 4721 (+88 into the body)

    Filters the category link.

  4. apply_filters( term_link )filterline 4733 (+100 into the body)

    Filters the term link.

Uses · 10

Used by · 18

Show all 18

Source code

function get_term_link( $term, $taxonomy = '' ) {	global $wp_rewrite; 	if ( ! is_object( $term ) ) {		if ( is_int( $term ) ) {			$term = get_term( $term, $taxonomy );		} else {			$term = get_term_by( 'slug', $term, $taxonomy );		}	} 	if ( ! is_object( $term ) ) {		$term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) );	} 	if ( is_wp_error( $term ) ) {		return $term;	} 	$taxonomy = $term->taxonomy; 	$termlink = $wp_rewrite->get_extra_permastruct( $taxonomy ); 	/**	 * Filters the permalink structure for a term before token replacement occurs.	 *	 * @since 4.9.0	 *	 * @param string  $termlink The permalink structure for the term's taxonomy.	 * @param WP_Term $term     The term object.	 */	$termlink = apply_filters( 'pre_term_link', $termlink, $term ); 	$slug = $term->slug;	$t    = get_taxonomy( $taxonomy ); 	if ( empty( $termlink ) ) {		if ( 'category' === $taxonomy ) {			$termlink = '?cat=' . $term->term_id;		} elseif ( $t->query_var ) {			$termlink = "?$t->query_var=$slug";		} else {			$termlink = "?taxonomy=$taxonomy&term=$slug";		}		$termlink = home_url( $termlink );	} else {		if ( ! empty( $t->rewrite['hierarchical'] ) ) {			$hierarchical_slugs = array();			$ancestors          = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );			foreach ( (array) $ancestors as $ancestor ) {				$ancestor_term        = get_term( $ancestor, $taxonomy );				$hierarchical_slugs[] = $ancestor_term->slug;			}			$hierarchical_slugs   = array_reverse( $hierarchical_slugs );			$hierarchical_slugs[] = $slug;			$termlink             = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink );		} else {			$termlink = str_replace( "%$taxonomy%", $slug, $termlink );		}		$termlink = home_url( user_trailingslashit( $termlink, 'category' ) );	} 	// Back compat filters.	if ( 'post_tag' === $taxonomy ) { 		/**		 * Filters the tag link.		 *		 * @since 2.3.0		 * @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter.		 * @since 5.4.1 Restored (un-deprecated).		 *		 * @param string $termlink Tag link URL.		 * @param int    $term_id  Term ID.		 */		$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );	} elseif ( 'category' === $taxonomy ) { 		/**		 * Filters the category link.

Changelog

Introduced in 2.5.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 6.7.7 tag, from src/wp-includes/taxonomy.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.