The get_post_type_archive_link() WordPress PHP function retrieves the permalink for a post type archive.
Usage
get_post_type_archive_link($post_type);
Example: get_post_type_archive_link(‘movies’) will return the archive link for the ‘movies’ post type.
Parameters
$post_type(string) – Required. The post type for which you want to get the archive link.
More information
See WordPress Developer Resources: get_post_type_archive_link()
Note: If get_post_type_archive_link() does not work or returns false, double check if you have included the argument has_archive => true when registering your post type.
Examples
Display a link to a ‘movies’ post type archive
This example shows how to display a link to the ‘movies’ post type archive.
// Display a link to the 'movies' post type archive
echo '<a href="' . get_post_type_archive_link('movies') . '">Movies Archive</a>';
Display a list of all registered post types with archive links
This example shows how to display a list of all registered post types and their archive links.
// Get all registered post types
$post_types = get_post_types(array('public' => true), 'objects');
// Loop through the post types and display archive links
foreach ($post_types as $post_type) {
$archive_link = get_post_type_archive_link($post_type->name);
if ($archive_link) {
echo '<a href="' . $archive_link . '">' . $post_type->label . ' Archive</a><br>';
}
}
Add a custom post type archive link to a menu
This example shows how to add a custom post type archive link to a WordPress menu using the wp_nav_menu_objects filter.
function add_movies_archive_link($items) {
$archive_link = get_post_type_archive_link('movies');
if ($archive_link) {
$movies_archive_item = array(
'title' => 'Movies Archive',
'url' => $archive_link,
'menu_order' => 1000,
'classes' => 'movies-archive-link',
);
$items[] = (object) $movies_archive_item;
}
return $items;
}
add_filter('wp_nav_menu_objects', 'add_movies_archive_link');
Display post type archive links using a shortcode
This example shows how to create a shortcode [post_type_archive_link post_type="movies"] that displays the archive link for the specified post type.
function post_type_archive_link_shortcode($atts) {
$atts = shortcode_atts(array('post_type' => 'post'), $atts, 'post_type_archive_link');
$archive_link = get_post_type_archive_link($atts['post_type']);
return $archive_link ? '<a href="' . $archive_link . '">' . ucwords($atts['post_type']) . ' Archive</a>' : '';
}
add_shortcode('post_type_archive_link', 'post_type_archive_link_shortcode');