get_term_link( WP_Term|int|string $term, string $taxonomy = '' ): string|WP_Error
- Since
- 2.5.0
- Source
wp-includes/taxonomy.php:4666
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
- Scaling
- Scales with input
- Instructions
- 12–103
- Plugin surface
- 4 hooks
- Called by
- 20
Reaches the database via get_term().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 159.
Third-party callbacks on 'pre_term_link', 'tag_link', 'category_link' run inside this call, and their cost is not bounded by anything here.
20 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_term()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
is_object($term) && is_wp_error() | 12 | is_wp_error() |
is_wp_error() | 20 | __(), is_wp_error() |
!is_int($term) && is_wp_error() | 20 | get_term_by(), is_wp_error() |
is_int($term) && is_wp_error() | 20 | get_term(), is_wp_error() |
!is_object($term) && !is_int($term) && is_wp_error() | 28 | get_term_by(), __(), is_wp_error() |
!is_object($term) && is_int($term) && is_wp_error() | 28 | get_term(), __(), is_wp_error() |
is_object($term) && !is_wp_error() && empty($termlink) && $taxonomy !== "category" && $taxonomy !== "post_tag" | 52–57 | is_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" | 58 | is_wp_error(), ->get_extra_permastruct(), apply_filters(), get_taxonomy(), user_trailingslashit(), home_url(), apply_filters() |
is_object($term) && !is_wp_error() && empty($termlink) | 59–65 | is_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–65 | get_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–65 | get_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–66 | is_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" | 66 | get_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" | 66 | get_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–73 | get_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–73 | get_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–73 | get_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–73 | get_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–74 | get_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–74 | get_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" | 74 | get_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" | 74 | get_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–81 | get_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–81 | get_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–79 | 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) | 81–82 | get_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–82 | get_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–87 | is_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–87 | get_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–87 | get_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–95 | get_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–95 | get_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–95 | get_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–95 | get_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–103 | get_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–103 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 159 | 12–103 | 12 | |
| 8.5 | 159 | 12–103 | 12 | |
| 8.4 | 159 | 12–103 | 12 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 168 | 12–109 | 12 | |
| 8.2 | 168 | 12–109 | 12 | |
| 8.1 | 168 | 12–109 | 12 | |
| 7.4 | 168 | 12–109 | 12 |
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:
- apply_filters( pre_term_link )filterline 4697 (+31 into the body)
Filters the permalink structure for a term before token replacement occurs.
Uses · 10
- get_term()Gets all term data from database by term ID.
- get_term_by()Gets all term data from database by term field and data.
- __()Retrieves the translation of $text.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- get_ancestors()Gets an array of ancestor IDs for a given object.
- user_trailingslashit()Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
- WP_Error::__construct()Initializes the error.
Used by · 20
- WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()Get the value emulated into a WP_Post and set up as a nav_menu_item.
- WP_Customize_Nav_Menus::load_available_items_query()Performs the post_type and taxonomy queries for loading available menu items.
- WP_Customize_Nav_Menus::search_available_items_query()Performs post queries for available-item searching.
- WP_REST_Term_Search_Handler::prepare_item()Prepares the search result for a given term ID.
- WP_REST_Terms_Controller::prepare_item_for_response()Prepares a single term output for response.
- WP_Sitemaps_Taxonomies::get_url_list()Gets a URL list for a taxonomy sitemap.
- WP_Terms_List_Table::handle_row_actions()Generates and displays row action links.
- Walker_Category::start_el()Starts the element output.
- _block_bindings_term_data_get_value()Gets value for Term Data source.
- get_category_link()Retrieves category link URL.
- get_post_format_link()Returns a link to a post format index.
- get_term_feed_link()Retrieves the feed link for a term.
Show all 20
- get_term_parents_list()Retrieves term parents with separator.
- get_the_taxonomies()Retrieves all taxonomies associated with a post.
- get_the_term_list()Retrieves a post's terms as a list with specified format.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
- render_block_core_term_name()Renders the `core/term-name` block on the server.
- wp_admin_bar_edit_menu()Provides an edit link for posts and terms.
- wp_setup_nav_menu_item()Decorates a menu item object with the shared navigation menu item properties.
- wp_tag_cloud()Displays a tag cloud.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.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.