get_post_type_archive_link

The timeline below displays how wordpress function get_post_type_archive_link has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.6

/**
 * Retrieves the permalink for a post type archive.
 *
 * @since 3.1.0
 * @since 4.5.0 Support for posts was added.
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param string $post_type Post type.
 * @return string|false The post type archive permalink. False if the post type
 *                      does not exist or does not have an archive.
 */
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);
}

WordPress Version: 5.4

/**
 * Retrieves the permalink for a post type archive.
 *
 * @since 3.1.0
 * @since 4.5.0 Support for posts was added.
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param string $post_type Post type.
 * @return string|false The post type archive permalink.
 */
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);
}

WordPress Version: 5.3

/**
 * Retrieves the permalink for a post type archive.
 *
 * @since 3.1.0
 * @since 4.5.0 Support for posts was added.
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param string $post_type Post type.
 * @return string|false The post type archive permalink.
 */
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);
}

WordPress Version: 4.6

/**
 * Retrieves the permalink for a post type archive.
 *
 * @since 3.1.0
 * @since 4.5.0 Support for posts was added.
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $post_type Post type.
 * @return string|false The post type archive permalink.
 */
function get_post_type_archive_link($post_type)
{
    global $wp_rewrite;
    if (!$post_type_obj = get_post_type_object($post_type)) {
        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);
}

WordPress Version: 4.5

/**
 * Retrieves the permalink for a post type archive.
 *
 * @since 3.1.0
 * @since 4.5.0 Support for posts was added.
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $post_type Post type.
 * @return string|false The post type archive permalink.
 */
function get_post_type_archive_link($post_type)
{
    global $wp_rewrite;
    if (!$post_type_obj = get_post_type_object($post_type)) {
        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);
    }
    /**
     * Filter 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);
}

WordPress Version: 4.3

/**
 * Retrieve the permalink for a post type archive.
 *
 * @since 3.1.0
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $post_type Post type
 * @return string|false The post type archive permalink.
 */
function get_post_type_archive_link($post_type)
{
    global $wp_rewrite;
    if (!$post_type_obj = get_post_type_object($post_type)) {
        return false;
    }
    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);
    }
    /**
     * Filter 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);
}

WordPress Version: 4.1

/**
 * Retrieve the permalink for a post type archive.
 *
 * @since 3.1.0
 *
 * @param string $post_type Post type
 * @return string The post type archive permalink.
 */
function get_post_type_archive_link($post_type)
{
    global $wp_rewrite;
    if (!$post_type_obj = get_post_type_object($post_type)) {
        return false;
    }
    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);
    }
    /**
     * Filter 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);
}

WordPress Version: 3.9

/**
 * Retrieve the permalink for a post type archive.
 *
 * @since 3.1.0
 *
 * @param string $post_type Post type
 * @return string
 */
function get_post_type_archive_link($post_type)
{
    global $wp_rewrite;
    if (!$post_type_obj = get_post_type_object($post_type)) {
        return false;
    }
    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);
    }
    /**
     * Filter 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);
}

WordPress Version: 3.7

/**
 * Retrieve the permalink for a post type archive.
 *
 * @since 3.1.0
 *
 * @param string $post_type Post type
 * @return string
 */
function get_post_type_archive_link($post_type)
{
    global $wp_rewrite;
    if (!$post_type_obj = get_post_type_object($post_type)) {
        return false;
    }
    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);
    }
    return apply_filters('post_type_archive_link', $link, $post_type);
}