What is get_term_link()?
The get_term_link() function is a core WordPress function that generates permalink URLs for taxonomy term archives. Whether you're working with built-in taxonomies like categories and tags or custom taxonomies in a plugin or theme, this function provides a reliable way to retrieve the correct URL for any term's archive page. Understanding how get_term_link works is essential for developers who need to create dynamic navigation, display term archives, or build custom taxonomy-based navigation systems.
This function has been part of WordPress since version 2.5.0 and remains a fundamental tool for theme and plugin development involving taxonomies. As WordPress has evolved through the years--from traditional themes to the modern block editor--get_term_link() has remained consistent, making it a reliable choice for any WordPress project.
Taxonomies in WordPress organize content in meaningful ways, and being able to dynamically generate links to these organizational groups is crucial for creating intuitive user experiences on any website. For comprehensive SEO optimization of your taxonomy pages, consider partnering with our SEO experts who understand how WordPress URL structures impact search visibility.
Core Functionality
What get_term_link Returns
The get_term_link() function returns a string containing the URL to a taxonomy term's archive page. On success, it returns the properly formatted permalink based on your site's URL structure and permalink settings. If the term doesn't exist or an error occurs, the function returns a WP_Error object, which allows you to gracefully handle errors in your code.
The function automatically handles different permalink structures, including hierarchical and non-hierarchical taxonomies:
- Hierarchical taxonomies (like categories): Builds URLs that reflect the term's parent-child relationship
- Non-hierarchical taxonomies (like tags): Creates simpler URL structures without hierarchy
How WordPress Constructs Term URLs
WordPress uses the $wp_rewrite global to construct term URLs based on your site's permalink settings. The function checks if a custom permalink structure exists for the taxonomy and builds the URL accordingly:
- Checks for a custom rewrite rule defined during taxonomy registration
- Applies any relevant filters
- Constructs the complete URL including the site domain and path components
If no custom structure is defined, it falls back to query-based URLs using the format ?taxonomy=taxonomy_name&term=term_slug.
// Example URL outputs based on taxonomy type
// Hierarchical taxonomy (category)
// URL: /category/technology/
$cat_link = get_term_link( 5, 'category' );
// Non-herarchical taxonomy (tag)
// URL: /tag/wordpress-tips/
$tag_link = get_term_link( 'wordpress-tips', 'post_tag' );
// With pretty permalinks disabled
// URL: /?taxonomy=category&term=technology
$query_link = get_term_link( 5, 'category' );
Parameters and Usage
Primary Parameter: $term
The $term parameter is highly flexible and accepts three different types of input:
| Type | Description |
|---|---|
| WP_Term object | Used directly without additional lookups |
| Integer (ID) | WordPress retrieves the term using get_term() |
| String (slug) | WordPress treats it as a slug and uses get_term_by() |
This flexibility makes the function convenient to use in various contexts throughout your WordPress development work. When you pass a WP_Term object, WordPress uses it directly. When you pass an integer, WordPress retrieves the term from the database. When you pass a string, WordPress treats it as a slug.
Secondary Parameter: $taxonomy
The $taxonomy parameter specifies which taxonomy to search within:
- Required when passing a term ID or slug
- Optional when passing a complete WP_Term object (taxonomy is already known from the term object)
When working with custom taxonomies, you must provide the taxonomy name exactly as it was registered, including any namespace or prefix.
Critical Type Conversion Note
Important: If you're working with term IDs stored as strings (common when data comes from forms, URLs, or databases), you must explicitly cast them to integers. Otherwise, WordPress will interpret the string as a slug rather than an ID.
// WRONG - will look for term with slug '5', not ID 5
$term_id = '5';
$link = get_term_link( $term_id, 'category' );
// CORRECT - explicitly cast to integer
$term_id = '5';
$link = get_term_link( (int) $term_id, 'category' );
This subtle behavior trips up many developers and can lead to unexpected 404 errors or broken links. According to the WordPress Developer Resources, this is one of the most common sources of confusion when working with term links.
Practical Examples
Basic Usage with Category
// Get link to a specific category by ID
$category_id = 5;
$category_link = get_term_link( $category_id, 'category' );
echo '<a href="' . esc_url( $category_link ) . '">View Category</a>';
Using Term Slug
// Get link to a tag by slug
$tag_slug = 'wordpress-tips';
$tag_link = get_term_link( $tag_slug, 'post_tag' );
if ( ! is_wp_error( $tag_link ) ) {
echo '<a href="' . esc_url( $tag_link ) . '">View Tag Archive</a>';
}
Working with Custom Taxonomies
// Get link to a custom taxonomy term
$product_category_id = 42;
$product_link = get_term_link( $product_category_id, 'product_category' );
if ( ! is_wp_error( $product_link ) ) {
echo '<a href="' . esc_url( $product_link ) . '">Browse Products</a>';
}
Handling Potential Errors
// Safe approach with error checking
$term_id = 15;
$term_link = get_term_link( $term_id, 'custom_taxonomy' );
if ( is_wp_error( $term_link ) ) {
// Handle error gracefully
error_log( 'Term link error: ' . $term_link->get_error_message() );
echo '<a href="/archive/">View All Items</a>';
} else {
echo '<a href="' . esc_url( $term_link ) . '">View Term</a>';
}
Dynamic Term Link Generation in Loops
// Get all terms from a post and display links
$post_terms = get_the_terms( get_the_ID(), 'category' );
if ( $post_terms && ! is_wp_error( $post_terms ) ) {
foreach ( $post_terms as $term ) {
$term_link = get_term_link( $term );
echo '<a href="' . esc_url( $term_link ) . '" class="term-link">'
. esc_html( $term->name ) . '</a>';
}
}
These examples demonstrate the function's flexibility across different use cases, from simple category links to complex custom taxonomy navigation systems commonly needed in WordPress development.
Best Practices
Always Check for WP_Error
Before using the returned URL, always verify it isn't a WP_Error object. Term links can fail if:
- The term doesn't exist
- The term was deleted
- There's a database issue
Failing to check for errors can expose users to broken pages or security vulnerabilities. The WP Kama documentation emphasizes this as a critical step in any implementation.
Use esc_url() for Output
Always wrap term links in esc_url() before outputting them in HTML attributes. This prevents XSS vulnerabilities and ensures proper URL encoding:
// UNSAFE - vulnerable to XSS
echo '<a href="' . $term_link . '">Link</a>';
// SAFE - properly escaped
echo '<a href="' . esc_url( $term_link ) . '">Link</a>';
Cast Numeric Term IDs
When working with term IDs from forms, URL parameters, or database results, explicitly cast them to integers using (int). This ensures WordPress treats them as IDs rather than slugs:
// Safe for any input source
$term_id = (int) $_GET['term_id'];
$link = get_term_link( $term_id, 'category' );
Cache Term Objects When Possible
If you need to get links for multiple terms in the same request, consider caching the term objects or URLs:
// Cache term object to avoid repeated DB queries
$term = get_term( $term_id, 'category' );
$term_link = get_term_link( $term );
Following these best practices ensures your term link implementations are secure, performant, and maintainable across different WordPress configurations and hosting environments.
Filter Hooks
term_link Filter
The term_link filter allows you to modify the generated URL before it's returned. This is useful for redirecting term archives, adding query parameters, or integrating with external systems:
add_filter( 'term_link', function( $termlink, $term, $taxonomy ) {
// Add tracking parameter to term links
return add_query_arg( 'source', 'navigation', $termlink );
}, 10, 3 );
Parameters passed to callback:
$termlink- The generated URL$term- The WP_Term object$taxonomy- The taxonomy name
pre_term_link Filter
The pre_term_link filter runs before WordPress constructs the URL structure, allowing you to modify the permalink template itself. This is more advanced and typically used for changing URL structures across an entire taxonomy:
add_filter( 'pre_term_link', function( $termlink, $term ) {
// Modify the URL structure before it's built
return str_replace( 'category/', 'topics/', $termlink );
}, 10, 2 );
Legacy Filters
WordPress also maintains legacy filters for backward compatibility:
- tag_link - Specifically for post_tag taxonomy
- category_link - Specifically for category taxonomy
These filters are still functional but modern code should use term_link for consistency across all taxonomies, including any custom taxonomies you create.
For developers working with custom fields and advanced WordPress configurations, these filters provide powerful ways to customize how term URLs are generated without modifying core WordPress behavior.
Common Mistakes and Troubleshooting
1. Incorrect Parameter Types
The most common mistake developers make is passing a string that looks like a number without casting it to an integer:
// PROBLEM: URL parameters are always strings
$term_id = $_GET['category']; // Returns '5' as string
// SOLUTION: Always cast numeric IDs
$term_id = (int) $_GET['category'];
$link = get_term_link( $term_id, 'category' );
When you pass '42' as a string, WordPress searches for a term with the slug '42' rather than ID 42. This is especially problematic when term IDs come from $_GET or $_POST superglobals, which always contain strings.
2. Missing Taxonomy Registration
If you're trying to get a term link for a taxonomy that hasn't been properly registered:
// ERROR: Taxonomy must be registered first
register_taxonomy( 'book_author', 'book' );
// Now terms can have their links generated
$link = get_term_link( $author_term_id, 'book_author' );
Ensure all custom taxonomies are registered before trying to generate term links.
3. Permalinks Not Configured
The get_term_link() function requires pretty permalinks to be enabled:
- Go to Settings → Permalinks in WordPress admin
- Select any option other than "Plain"
- Save changes to flush rewrite rules
Without pretty permalinks, term links will use query-based URLs like ?taxonomy=category&term=uncategorized. While functional, these URLs are less SEO-friendly and provide a poorer user experience.
4. Term Doesn't Exist
Always verify the term exists before generating a link:
$term = get_term( $term_id, 'category' );
if ( $term && ! is_wp_error( $term ) ) {
$link = get_term_link( $term );
}
Following these troubleshooting steps will help you quickly identify and resolve common issues when working with term links in WordPress.
Integration with Modern WordPress Development
Block Theme Integration
When building custom blocks that display term links, get_term_link() works seamlessly within PHP-based blocks and can be used to generate URLs that frontend JavaScript can consume:
// PHP block registration
register_block_type( 'my-plugin/term-list', array(
'render_callback' => function( $attributes ) {
$terms = get_the_terms( get_the_ID(), 'category' );
$output = '<div class="term-links">';
foreach ( $terms as $term ) {
$output .= '<a href="' . esc_url( get_term_link( $term ) ) . '">';
$output .= esc_html( $term->name ) . '</a>';
}
$output .= '</div>';
return $output;
},
) );
This function is particularly useful for Query Loop block variations or custom navigation blocks in modern WordPress development using the block editor.
REST API Contexts
When building REST API endpoints that need to include term URLs:
// Add term links to REST API response
add_action( 'rest_api_init', function() {
register_rest_field( 'post', 'category_links', array(
'get_callback' => function( $post_arr ) {
$terms = get_the_terms( $post_arr['id'], 'category' );
return array_map( function( $term ) {
return get_term_link( $term );
}, $terms ? $terms : [] );
},
) );
} );
Headless WordPress
In headless WordPress setups with frontend frameworks like Next.js or Gatsby, get_term_link() can still be used on the WordPress backend to generate URLs that are then passed to the frontend via the REST API or GraphQL. This allows you to maintain proper URL structures while leveraging modern JavaScript frameworks for the frontend.
For developers exploring headless architectures, understanding how get_term_link() generates URLs is essential for building proper navigation systems that work seamlessly across the WordPress backend and your chosen frontend framework.
Summary
The get_term_link() function is an essential tool for WordPress developers working with taxonomies. Its flexibility in accepting different parameter types, combined with proper error handling, makes it reliable for both simple and complex use cases.
Key Takeaways
- Flexible Parameters - Accepts WP_Term objects, integer IDs, or string slugs
- Always Check Errors - Use
is_wp_error()before using the returned URL - Escape Outputs - Wrap URLs in
esc_url()for security - Cast Numeric IDs - Use
(int)when term IDs might be strings - Use Filters - Leverage
term_linkfilter for URL customization
When to Use get_term_link()
- Creating navigation menus with taxonomy archives
- Displaying term links in post templates
- Building custom taxonomy-based filters
- Generating URLs for dynamic content archives
- REST API responses needing term archive links
By understanding how WordPress constructs term URLs and following these best practices, you can build robust taxonomy navigation systems that work seamlessly across different WordPress configurations. Whether you're working with standard categories and tags or complex custom taxonomies, get_term_link() provides the foundation for effective term archive navigation.
For developers looking to expand their WordPress capabilities, combining get_term_link() with custom fields and other advanced WordPress features opens up powerful possibilities for content organization and user navigation.
Frequently Asked Questions
Sources
- WordPress Developer Resources - get_term_link() - Official function documentation, parameters, and return values
- WP Kama - get_term_link() Function - Usage examples, common mistakes, and filter hooks
- WPTurbo - get_term_link WordPress Function - Developer resource with quick reference and examples