get_the_term_list( int $post_id, string $taxonomy, string $before = '', string $sep = '', string $after = '' ): string|false|WP_Error
- Since
- 2.5.0
- Source
wp-includes/category-template.php:1338
Description
Terms are linked to their respective term listing pages.
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
$post_idint- Post ID.
$taxonomystring- Taxonomy name.
$beforestringoptional- String to use before the terms. Default empty.Default:
'' $sepstringoptional- String to use between the terms. Default empty.Default:
'' $afterstringoptional- String to use after the terms. Default empty.Default:
''
Return value
string|false|WP_Error- A list of terms on success, false if there are no terms, WP_Error on failure.
Performance profile
How much work a call to get_the_term_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
- Scaling
- Scales with input
- Instructions
- 15–30
- Plugin surface
- 1 hook
- Called by
- 3
Reaches the database via get_post().
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 54.
Third-party callbacks on 'term_links-{$taxonomy}' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()one call below get_the_term_list() - cacheobject cache
wp_cache_add()one call below get_the_term_list()
Further down the call graph this can also reach option, 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_the_term_list() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–17 | get_the_terms(), is_wp_error() |
!is_wp_error() && !empty($terms) | 29–30 | get_the_terms(), is_wp_error(), apply_filters() |
!empty($terms) && !$terms | 30 | get_the_terms(), is_wp_error(), get_term_link(), is_wp_error() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 54 | 15–30 | 5 | |
| 8.5 | 54 | 15–30 | 5 | |
| 8.4 | 54 | 15–30 | 5 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 57 | 15–33 | 5 | |
| 8.2 | 57 | 15–33 | 5 | |
| 8.1 | 57 | 15–33 | 5 | |
| 7.4 | 57 | 15–33 | 5 |
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 · 1
One hook fires while get_the_term_list() runs, in this order:
- apply_filters( term_links-{$taxonomy} )filterline 1375 (+37 into the body)
Filters the term links for a given taxonomy.
Uses · 5
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_term_link()Generates a permalink for a taxonomy term archive.
- esc_url()Checks and cleans a URL.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 3
- get_the_tag_list()Retrieves the tags for a post formatted as a string.
- render_block_core_post_terms()Renders the `core/post-terms` block on the server.
- the_terms()Displays the terms for a post in a list.
Source code
function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) { $terms = get_the_terms( $post_id, $taxonomy ); if ( is_wp_error( $terms ) ) { return $terms; } if ( empty( $terms ) ) { return false; } $links = array(); foreach ( $terms as $term ) { $link = get_term_link( $term, $taxonomy ); if ( is_wp_error( $link ) ) { return $link; } $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; } /** * Filters the term links for a given taxonomy. * * The dynamic portion of the hook name, `$taxonomy`, refers * to the taxonomy slug. * * Possible hook names include: * * - `term_links-category` * - `term_links-post_tag` * - `term_links-post_format` * * @since 2.5.0 * * @param string[] $links An array of term links. */ $term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores return $before . implode( $sep, $term_links ) . $after;}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 7.0.4 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.