wordpress-seo
wordpress-seo copied to clipboard
Primary taxonomy item is not respected in permalink
- [x] I've read and understood the contribution guidelines.
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Please give us a description of what happened.
When creating or updating an item in a custom post type, the permalink does not reflect the "primary" category.
Please describe what you expected to happen and why.
When creating or updating an item in a custom post type, the permalink should reflect the "primary" category. This is similar to #11006 and #4528, but does not use WooCommerce.
How can we reproduce this behavior?
- Add a custom post type and custom taxonomy.
/**
* Register Custom Post Type "Products".
*/
function example_create_products_post_type() {
$labels = array(
'name' => _x( 'Products', 'Post Type General Name', 'example' ),
'singular_name' => _x( 'Product', 'Post Type Singular Name', 'example' ),
'menu_name' => __( 'Products', 'example' ),
'name_admin_bar' => __( 'Product', 'example' ),
'archives' => __( 'Product List', 'example' ),
'parent_item_colon' => __( 'Parent Product:', 'example' ),
'all_items' => __( 'All Products', 'example' ),
'add_new_item' => __( 'Add New Product', 'example' ),
'add_new' => __( 'Add New', 'example' ),
'new_item' => __( 'New Product', 'example' ),
'edit_item' => __( 'Edit Product', 'example' ),
'update_item' => __( 'Update Product', 'example' ),
'view_item' => __( 'View Product', 'example' ),
'search_items' => __( 'Search Product', 'example' ),
'not_found' => __( 'Not found', 'example' ),
'not_found_in_trash' => __( 'Not found in Trash', 'example' ),
'featured_image' => __( 'Featured Image', 'example' ),
'set_featured_image' => __( 'Set featured image', 'example' ),
'remove_featured_image' => __( 'Remove featured image', 'example' ),
'use_featured_image' => __( 'Use as featured image', 'example' ),
'insert_into_item' => __( 'Insert into product', 'example' ),
'uploaded_to_this_item' => __( 'Uploaded to this product', 'example' ),
'items_list' => __( 'Product list', 'example' ),
'items_list_navigation' => __( 'Product list navigation', 'example' ),
'filter_items_list' => __( 'Filter product list', 'example' ),
);
$args = array(
'label' => __( 'Products', 'example' ),
'labels' => $labels,
'rewrite' => array( 'slug' => 'products/%products_category%' ),
'supports' => array( 'title', 'editor', 'revisions', 'thumbnail', ),
'taxonomies' => array( 'products_category' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-cart',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'products',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'products', $args );
}
add_action( 'init', 'example_create_products_post_type', 0 );
/**
* Register Custom Taxonomy "Category" for "Products" Post Type.
*/
function example_create_products_category() {
$labels = array(
'name' => _x( 'Product Categories', 'Taxonomy General Name', 'example' ),
'singular_name' => _x( 'Product Category', 'Taxonomy Singular Name', 'example' ),
'menu_name' => __( 'Product Categories', 'example' ),
'all_items' => __( 'All Product Categories', 'example' ),
'parent_item' => __( 'Parent Product Category', 'example' ),
'parent_item_colon' => __( 'Parent Product Category:', 'example' ),
'new_item_name' => __( 'New Product Category Name', 'example' ),
'add_new_item' => __( 'Add New Product Category', 'example' ),
'edit_item' => __( 'Edit Product Category', 'example' ),
'update_item' => __( 'Update Product Category', 'example' ),
'view_item' => __( 'View Product Category', 'example' ),
'separate_items_with_commas' => __( 'Separate product categories with commas', 'example' ),
'add_or_remove_items' => __( 'Add or remove product categories', 'example' ),
'choose_from_most_used' => __( 'Choose from the most used product categories', 'example' ),
'popular_items' => __( 'Popular Product Categories', 'example' ),
'search_items' => __( 'Search product categories', 'example' ),
'not_found' => __( 'Not Found', 'example' ),
'no_terms' => __( 'No product categories', 'example' ),
'items_list' => __( 'Product Categories list', 'example' ),
'items_list_navigation' => __( 'Product Categories list navigation', 'example' ),
);
$args = array(
'labels' => $labels,
'rewrite' => array( 'slug' => 'products', 'hierarchical' => false ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'products_category', array( 'products' ), $args );
}
add_action( 'init', 'example_create_products_category', 0 );
- Add an item into the CPT and select multiple categories and choose one as a primary category.
- See that the permalink is incorrect.
Technical info
- If relevant, which editor is affected (or editors):
- [x] Classic Editor
- [x] Gutenberg
- [x] Classic Editor plugin
- Which browser is affected (or browsers):
- [x] Chrome
- [x] Firefox
- [x] Safari
- [ ] Other
Used versions
- WordPress version: 5.4.1
- Yoast SEO version: 14.3
- Gutenberg plugin version: N/A
- Classic Editor plugin version: 1.5
- Relevant plugins in case of a bug:
- Tested with theme: twentyseventeen
Confirmed, though seeing as this is a custom posttype I'm not sure we'll be fixing this on our end.
I don't understand why this would not be something that Yoast would fix. This isn't a secondary plugin causing a problem, this is the core plugin functionality not working. If "Primary" is not supported with custom taxonomies/post types, then you should disable the functionality in those cases, otherwise, I see no reason to not expect it to work.
I've worked to implement a temporary workaround. For others experiencing this issue, this workaround works for me (although I did some further customizations to properly handle some edge cases).
Make sure if you use the below function to replace example
with the name of your CPT and example_taxonomy
with the name of your custom taxonomy.
function yoast_seo_primary_cpt_workaround( $url, $post ) {
if ( class_exists( 'WPSEO_Primary_Term' ) && $post->post_type == 'example' ) {
$primary_term = new WPSEO_Primary_Term( 'example_taxonomy', $post->ID );
$primary_term_id = $primary_term->get_primary_term();
$cat_link = get_term_link( $primary_term_id );
if ( ! is_wp_error( $cat_link ) ) {
$url = trailingslashit( $cat_link . $post->post_name );
}
}
return $url;
}
add_filter( 'post_type_link', 'yoast_seo_primary_cpt_workaround', 10, 2 );
Hopefully Yoast will step up to the plate and fix this bug, but based off the responses I've received from support and the comment on this post, I'm not hopeful it will get fixed.
Same issue here. Hopefully Yaost Dev team is willing to have a look at it and fix this. Thanks ;)
Same issue on my website. I think CPTs are a very basic feature of WP Core and don't understand why the primary Term option is displayed on CPTs even though it isn't working.
Also some themes rely on things like: class_exists('WPSEO_Primary_Term')
Just to take this into consideration when (hopefully) reevaluating this issue.
Here for this same issue, being that Yoast labels itself as an SEO tool. I think this is honestly a major issue with the tool
I've worked to implement a temporary workaround. For others experiencing this issue, this workaround works for me (although I did some further customizations to properly handle some edge cases).
Make sure if you use the below function to replace
example
with the name of your CPT andexample_taxonomy
with the name of your custom taxonomy.function yoast_seo_primary_cpt_workaround( $url, $post ) { if ( class_exists( 'WPSEO_Primary_Term' ) && $post->post_type == 'example' ) { $primary_term = new WPSEO_Primary_Term( 'example_taxonomy', $post->ID ); $primary_term_id = $primary_term->get_primary_term(); $cat_link = get_term_link( $primary_term_id ); if ( ! is_wp_error( $cat_link ) ) { $url = trailingslashit( $cat_link . $post->post_name ); } } return $url; } add_filter( 'post_type_link', 'yoast_seo_primary_cpt_workaround', 10, 2 );
Hopefully Yoast will step up to the plate and fix this bug, but based off the responses I've received from support and the comment on this post, I'm not hopeful it will get fixed.
Thanks for this solution @michaelryanmcneill i've just used it succesfully, you saved my bacon late on a Friday!
Why this issue still not be solved??
Thanks @michaelryanmcneill , your solution works.
+1 https://wordpress.org/support/topic/primary-cpt-taxonomy-isnt-correctly-selected/
Please inform the customer of conversation # 1067748 when this conversation has been closed.