get_post_type_archive_link( string $post_type ): string|false
- Since
- 3.1.0, 4.5.0
- Source
wp-includes/link-template.php:1301
Compatibility
- WordPress
- since 4.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_typestring- Post type.
Return value
string|false- The post type archive permalink. False if the post type does not exist or does not have an archive.
Performance profile
How much work a call to get_post_type_archive_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
- Constant
- Instructions
- 8–47
- Plugin surface
- 1 hook
- Called by
- 10
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 83.
Third-party callbacks on 'post_type_archive_link' run inside this call, and their cost is not bounded by anything here.
10 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below get_post_type_archive_link() - serializeserialisation
maybe_unserialize()one call below get_post_type_archive_link() - querycontent query
get_post()one call below get_post_type_archive_link()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_type_archive_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–12 | get_post_type_object() |
$post_type !== "post" && $post_type_obj | 26–29 | get_post_type_object(), get_option(), home_url(), apply_filters() |
$post_type === "post" | 28–29 | get_post_type_object(), get_option(), get_option(), get_home_url(), apply_filters() |
$post_type === "post" && $show_on_front === "page" | 31 | get_post_type_object(), get_option(), get_option(), get_permalink(), apply_filters() |
$post_type !== "post" && $post_type_obj && get_option() | 44–47 | get_post_type_object(), get_option(), user_trailingslashit(), home_url(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 83 | 8–47 | 9 | |
| 8.5 | 83 | 8–47 | 9 | |
| 8.4 | 83 | 8–47 | 9 | |
| 8.3 | 83 | 8–47 | 9 | |
| 8.2 | 83 | 8–47 | 9 | |
| 8.1 | 83 | 8–47 | 9 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 85 | 8–48 | 9 |
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 · 2
2 hooks fire while get_post_type_archive_link() runs, in this order:
- apply_filters( post_type_archive_link )filterline 1320 (+19 into the body)
Filters the post type archive permalink.
- apply_filters( post_type_archive_link )filterline 1347 (+46 into the body)
Filters the post type archive permalink.
Uses · 7
- get_post_type_object()Retrieves a post type object by name.
- get_option()Retrieves an option value based on an option name.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_home_url()Retrieves the URL for a given site where the front end is accessible.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- user_trailingslashit()Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
Used by · 10
- 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.
- get_post_type_archive_feed_link()Retrieves the permalink for a post type archive feed.
- render_block_core_breadcrumbs()Renders the `core/breadcrumbs` block on the server.
- render_block_core_navigation_link()Renders the `core/navigation-link` block.
- render_block_core_navigation_submenu()Renders the `core/navigation-submenu` block.
- wp_admin_bar_edit_menu()Provides an edit link for posts and terms.
- wp_list_categories()Displays or retrieves the HTML list of categories.
- wp_nav_menu_item_post_type_meta_box()Displays a meta box for a post type menu item.
- wp_setup_nav_menu_item()Decorates a menu item object with the shared navigation menu item properties.
Source code
function get_post_type_archive_link( $post_type ) { global $wp_rewrite; $post_type_obj = get_post_type_object( $post_type ); if ( ! $post_type_obj ) { return false; } if ( 'post' === $post_type ) { $show_on_front = get_option( 'show_on_front' ); $page_for_posts = get_option( 'page_for_posts' ); if ( 'page' === $show_on_front && $page_for_posts ) { $link = get_permalink( $page_for_posts ); } else { $link = get_home_url(); } /** This filter is documented in wp-includes/link-template.php */ return apply_filters( 'post_type_archive_link', $link, $post_type ); } if ( ! $post_type_obj->has_archive ) { return false; } if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) { $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive; if ( $post_type_obj->rewrite['with_front'] ) { $struct = $wp_rewrite->front . $struct; } else { $struct = $wp_rewrite->root . $struct; } $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) ); } else { $link = home_url( '?post_type=' . $post_type ); } /** * Filters the post type archive permalink. * * @since 3.1.0 * * @param string $link The post type archive permalink. * @param string $post_type Post type name. */ return apply_filters( 'post_type_archive_link', $link, $post_type );}Changelog
Introduced in 3.1.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.1.0 tag, from
src/wp-includes/link-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.