get_post_type_archive_link()

You are here:
  • Help
  • get_post_type_archive_link()

get_post_type_archive_link( string $post_type )

Retrieves the permalink for a post type archive.

Parameters Parameters

$post_type

(string) (Required) Post type.


Top ↑

Return Return

(string|false) The post type archive permalink.


Top ↑

Source Source

File: wp-includes/link-template.php

1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
4.5.0 Support for posts was added.
3.1.0 Introduced.
Was this article helpful?
Dislike 0
Views: 6