If you want to redirect a taxonomy page to it's post type archive, you might try something like below.
Careful, though! There's an assumption implicit in your post that a WP_Taxonomy has a single post_type mapped to it. In practice, you can have your taxonomies map to as many post_type objects as you like. The code below redirects taxonomy pages to an associated custom post type archive wether or not it's the only one that taxonomy applies to!
Additionally – make sure that your custom post type's got the has_archive key set to true in it's register_post_type args!
<?php
// functions.php
function redirect_cpt_taxonomy(WP_Query $query) {
// Get the object of the query. If it's a `WP_Taxonomy`, then you'll be able to check if it's registered to your custom post type!
$obj = get_queried_object();
if (!is_admin() // Not admin
&& true === $query->is_main_query() // Not a secondary query
&& true === $query->is_tax() // Is a taxonomy query
) {
$archive_url = get_post_type_archive_link('custom_post_type_name');
// If the WP_Taxonomy has multiple object_types mapped, and 'custom_post_type' is one of them:
if (true === is_array($obj->object_type) && true === in_array($obj->object_type, ['custom_post_type_name'])) {
wp_redirect($archive_url);
exit;
}
// If the WP_Taxonomy has one object_type mapped, and it's 'custom_post_type'
if (true === is_string($obj->object_type) && 'custom_post_type_name' === $obj->object_type) {
wp_redirect($archive_url);
exit;
}
}
// Passthrough, if none of these conditions are met!
return $query;
}
add_action('pre_get_posts', 'redirect_cpt_taxonomy');
301response), or 2) alter the content of the taxonomy url to look like the main archive page? I think #1 is preferable! - Cameron Hurd