custom post type unique slug causing issues with post id
I’ve included all of the relevant code below. Here is what I’m trying to accomplish: A custom taxonomy of flavor. A custom post type of product. The ability to have flavor/product-name and another-flavor/product name vs product-name & product-name-2. What I’m experiencing with the code below is on the front end the 2nd product with the same name is taking the post ID of the first product. So if I create flavor/product-name and then create another-flavor/product-name, when I view the post on the front end, all of the content is from flavor/product-name instead of another-flavor/product-name when I view another-flavor/product-name. On the backend the IDs are showing correctly, but something is getting hijacked on the front end. But if I create flavor/another-product-name then that post works fine, but another-flavor/another-product-name is broken just like another-flavor/product-name.
add_action( 'init', $n( 'taxonomies' ) );
add_action( 'init', $n( 'post_types' ) );
add_filter( 'post_type_link', $n( 'product_post_type_link' ), 10, 3 );
add_filter( 'wp_unique_post_slug', $n( 'product_disable_unique_slug' ), 11, 6 );
add_filter( 'the_title', $n( 'admin_product_title' ), 10, 2 );
add_filter( 'body_class', $n( 'product_body_class' ) );
function taxonomies() {
/**
* Taxonomy: Flavors.
*/
$labels = [
"name" => __( "Flavors", "puff_theme" ),
"singular_name" => __( "Flavor", "puff_theme" ),
"menu_name" => __( "Flavors", "puff_theme" ),
"all_items" => __( "All Flavors", "puff_theme" ),
"edit_item" => __( "Edit Flavor", "puff_theme" ),
"view_item" => __( "View Flavor", "puff_theme" ),
"update_item" => __( "Update Flavor name", "puff_theme" ),
"add_new_item" => __( "Add new Flavor", "puff_theme" ),
"new_item_name" => __( "New Flavor name", "puff_theme" ),
"parent_item" => __( "Parent Flavor", "puff_theme" ),
"parent_item_colon" => __( "Parent Flavor:", "puff_theme" ),
"search_items" => __( "Search Flavors", "puff_theme" ),
"popular_items" => __( "Popular Flavors", "puff_theme" ),
"separate_items_with_commas" => __( "Separate Flavors with commas", "puff_theme" ),
"add_or_remove_items" => __( "Add or remove Flavors", "puff_theme" ),
"choose_from_most_used" => __( "Choose from the most used Flavors", "puff_theme" ),
"not_found" => __( "No Flavors found", "puff_theme" ),
"no_terms" => __( "No Flavors", "puff_theme" ),
"items_list_navigation" => __( "Flavors list navigation", "puff_theme" ),
"items_list" => __( "Flavors list", "puff_theme" ),
];
$args = [
"label" => __( "Flavors", "puff_theme" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => false,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => false,
"query_var" => true,
"rewrite" => [ 'slug' => 'flavor' ],
"show_admin_column" => true,
"show_in_rest" => true,
"rest_base" => "flavor",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
];
register_taxonomy( "flavor", [ "product" ], $args );
}
function post_types() {
/**
* Post Type: Products.
*/
$labels = [
"name" => __( "Products", "puff_theme" ),
"singular_name" => __( "Product", "puff_theme" ),
"menu_name" => __( "Products", "puff_theme" ),
"all_items" => __( "All Products", "puff_theme" ),
"add_new" => __( "Add new", "puff_theme" ),
"add_new_item" => __( "Add new Product", "puff_theme" ),
"edit_item" => __( "Edit Product", "puff_theme" ),
"new_item" => __( "New Product", "puff_theme" ),
"view_item" => __( "View Product", "puff_theme" ),
"view_items" => __( "View Products", "puff_theme" ),
"search_items" => __( "Search Products", "puff_theme" ),
"not_found" => __( "No Products found", "puff_theme" ),
"not_found_in_trash" => __( "No Products found in trash", "puff_theme" ),
"parent" => __( "Parent Product:", "puff_theme" ),
"featured_image" => __( "Featured image for this Product", "puff_theme" ),
"set_featured_image" => __( "Set featured image for this Product", "puff_theme" ),
"remove_featured_image" => __( "Remove featured image for this Product", "puff_theme" ),
"use_featured_image" => __( "Use as featured image for this Product", "puff_theme" ),
"archives" => __( "Products", "puff_theme" ),
"insert_into_item" => __( "Insert into Product", "puff_theme" ),
"uploaded_to_this_item" => __( "Upload to this Product", "puff_theme" ),
"filter_items_list" => __( "Filter Products list", "puff_theme" ),
"items_list_navigation" => __( "Products list navigation", "puff_theme" ),
"items_list" => __( "Products list", "puff_theme" ),
"attributes" => __( "Products attributes", "puff_theme" ),
"name_admin_bar" => __( "Product", "puff_theme" ),
"item_published" => __( "Product published", "puff_theme" ),
"item_published_privately" => __( "Product published privately.", "puff_theme" ),
"item_reverted_to_draft" => __( "Product reverted to draft.", "puff_theme" ),
"item_scheduled" => __( "Product scheduled", "puff_theme" ),
"item_updated" => __( "Product updated.", "puff_theme" ),
"parent_item_colon" => __( "Parent Product:", "puff_theme" ),
];
$args = [
"label" => __( "Products", "puff_theme" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => "products",
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => [ "slug" => "products/%flavor%", "with_front" => false ],
"query_var" => true,
"menu_icon" => "dashicons-products",
"supports" => [ "title", "editor", "thumbnail" ],
];
register_post_type( "product", $args );
}
function product_post_type_link( $permalink, $post, $leavename ) {
if( 'product' === $post->post_type ) {
$flavor = get_field( 'flavor', $id );
if( !empty( $flavor ) ) {
$permalink = home_url( "products/" . $flavor->slug . "/" . $post->post_name . "/" );
} else {
$permalink = home_url( "products/none/" . $post->post_name . "/" );
}
}
return $permalink;
}
function product_disable_unique_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ){
if( 'product' === $post_type ){
$slug = $original_slug;
}
return $slug;
}
function admin_product_title( $title, $id ) {
if( is_admin() && 'product' === get_post_type( $id ) ) {
$flavor = get_field( 'flavor', $id );
return sprintf( '%s %s', $flavor->name, $title );
}
return $title;
}
function product_body_class( $classes ) {
if( is_admin() ) {
return $classes;
}
if( is_singular( 'product' ) ) {
$flavor = Helpersget_flavor();
$color = get_field( 'color', $flavor );
$classes[] = sprintf( 'theme-%s', $color );
}
return $classes;
}
Leave an answer