• Hi,

    So at the moment, I have a custom taxonomy ‘platforms’ with terms like:

    • iOS
    • Web
    • Android

    Archive slugs are like this: /platform/ios/

    Now, I was thinking of creating a custom ‘Platform’ post type for profiling apps and websites. This would have the same slug format (e.g. /platform/twitter/, /platform/wordpress/, etc.)

    The idea here is that while iOS, Android, etc. are their own platforms, apps and websites are platforms built on top of those platforms. Because of that, I’m really not sure of any other way to word things, so changing the name of either the post type of taxonomy doesn’t seem to be an ideal option (open to ideas though if you have any!).

    My question is:

    What are the problems that could arise with this setup? Are there any easy solutions?

    If need be, I can disable the archives for the ‘platform’ taxonomy, as the custom post type would be more important. Would that help?

    Any help or thoughts are appreciated. Thanks!

    • This topic was modified 2 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @dylanhoulihan,

    In a situation where the slug of a CPT (Custom Post Type) and a Taxonomy is same, precedence will be given to whichever is registered first.

    For example, if Taxonomy platform is registered before CPT platform, then the taxonomy archive page will work as expected, but a CPT post will throw a 404 and vice-versa.

    If CPT is more important and if you are okay with a different slug for the taxonomy while keeping the labels same for both, you can use the rewrite parameter to modify the slug as follows:

    function register_custom_taxonomy() {
     
        $args = array(
            ...
            ...
            ...
            'rewrite' => array( 'slug' => 'platform-taxonomy' ),
        );
     
        register_taxonomy( 'platform', 'post', $args );
     
    }
    add_action( 'init', 'register_custom_taxonomy' );
    Thread Starter Dylan Houlihan

    (@dylanhoulihan)

    Thanks @kaavyaiyer, that’s really good to know.

    The CPT is going to be more important, but it’s not registered yet. I already registered the taxonomy though.

    If I implement the slug rewrite and then create the custom post type, will that work out smoothly? Or is there a certain order I should do this in?

    Hi @dylanhoulihan

    The order of registration only matters when the slug for both CPT and taxonomy are same. The one which is registered first will work properly while the other will throw a 404.

    Both functions – register_taxonomy and register_post_type accept an argument to rewrite slug during the time of registration.

    Rewriting slug during taxonomy’s registration and then registering a CPT should not create any problem.

    Your code should look something like this:

    /**
     * Register the 'platform' taxonomy with 'platform-taxonomy' rewrite.
     */
    function register_taxonomy() {
     
        $args = array(
            // other arguments...
            'rewrite' => array( 'slug' => 'platform-taxonomy' ), // taxonomy will use the <code>platform-taxonomy</code> slug.
        );
     
        register_taxonomy( 'platform', 'post', $args );
    }
    
    add_action( 'init', 'register_taxonomy', 0 );
    
    /**
     * Register the 'platform' post type.
     */
    function register_cpt() {
        $args = array(
            // arguments...
        );
        register_post_type( 'platform', $args ); // CPT will use the <code>platform</code> slug.
    }
    add_action( 'init', 'register_cpt' );
    Thread Starter Dylan Houlihan

    (@dylanhoulihan)

    @kaavyaiyer Thanks for your help! Will let you know if I have any issues once I implement this.

    Thread Starter Dylan Houlihan

    (@dylanhoulihan)

    Hey @kaavyaiyer so I created my Platform post type after setting a custom rewrite slug for the platforms taxonomy to “platform-taxonomy”

    Unfortunately, after creating the platform post type, my platform taxonomy links still all broke and the rewrite slug just stopped working.

    Even weirder and more problematic, the Platform taxonomy terms I’ve created won’t show up on Platform post-type pages.

    Here’s a screenshot to show you what I mean.

    The taxonomy terms show up on their page (see here), so I’m not sure why this is happening.

    Any advice?

    If it makes a difference, I’m using the CPT UI plugin to create the custom taxonomies and post types.

    Here’s the platform taxonomy code:

    function cptui_register_my_taxes_platform() {
    
    	/**
    	 * Taxonomy: Platforms.
    	 */
    
    	$labels = [
    		"name" => __( "Platforms", "smart-passive-income-pro" ),
    		"singular_name" => __( "Platform", "smart-passive-income-pro" ),
    		"menu_name" => __( "Platforms", "smart-passive-income-pro" ),
    		"all_items" => __( "All Platforms", "smart-passive-income-pro" ),
    		"edit_item" => __( "Edit Platform", "smart-passive-income-pro" ),
    		"view_item" => __( "View Platform", "smart-passive-income-pro" ),
    		"update_item" => __( "Update Platform name", "smart-passive-income-pro" ),
    		"add_new_item" => __( "Add new Platform", "smart-passive-income-pro" ),
    		"new_item_name" => __( "New Platform name", "smart-passive-income-pro" ),
    		"parent_item" => __( "Parent Platform", "smart-passive-income-pro" ),
    		"parent_item_colon" => __( "Parent Platform:", "smart-passive-income-pro" ),
    		"search_items" => __( "Search Platforms", "smart-passive-income-pro" ),
    		"popular_items" => __( "Popular Platforms", "smart-passive-income-pro" ),
    		"separate_items_with_commas" => __( "Separate Platforms with commas", "smart-passive-income-pro" ),
    		"add_or_remove_items" => __( "Add or remove Platforms", "smart-passive-income-pro" ),
    		"choose_from_most_used" => __( "Choose from the most used Platforms", "smart-passive-income-pro" ),
    		"not_found" => __( "No Platforms found", "smart-passive-income-pro" ),
    		"no_terms" => __( "No Platforms", "smart-passive-income-pro" ),
    		"items_list_navigation" => __( "Platforms list navigation", "smart-passive-income-pro" ),
    		"items_list" => __( "Platforms list", "smart-passive-income-pro" ),
    		"back_to_items" => __( "Back to Platforms", "smart-passive-income-pro" ),
    	];
    
    	
    	$args = [
    		"label" => __( "Platforms", "smart-passive-income-pro" ),
    		"labels" => $labels,
    		"public" => true,
    		"publicly_queryable" => true,
    		"hierarchical" => true,
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => [ 'slug' => 'platform-taxonomy', 'with_front' => true, ],
    		"show_admin_column" => false,
    		"show_in_rest" => true,
    		"show_tagcloud" => false,
    		"rest_base" => "platform",
    		"rest_controller_class" => "WP_REST_Terms_Controller",
    		"show_in_quick_edit" => false,
    		"show_in_graphql" => false,
    	];
    	register_taxonomy( "platform", [ "post", "platform" ], $args );
    }
    add_action( 'init', 'cptui_register_my_taxes_platform' );
    

    And here’s the platforms post type code:

    function cptui_register_my_cpts_platform() {
    
    	/**
    	 * Post Type: Platforms.
    	 */
    
    	$labels = [
    		"name" => __( "Platforms", "smart-passive-income-pro" ),
    		"singular_name" => __( "Platform", "smart-passive-income-pro" ),
    		"menu_name" => __( "My Platforms", "smart-passive-income-pro" ),
    		"all_items" => __( "All Platforms", "smart-passive-income-pro" ),
    		"add_new" => __( "Add new", "smart-passive-income-pro" ),
    		"add_new_item" => __( "Add new Platform", "smart-passive-income-pro" ),
    		"edit_item" => __( "Edit Platform", "smart-passive-income-pro" ),
    		"new_item" => __( "New Platform", "smart-passive-income-pro" ),
    		"view_item" => __( "View Platform", "smart-passive-income-pro" ),
    		"view_items" => __( "View Platforms", "smart-passive-income-pro" ),
    		"search_items" => __( "Search Platforms", "smart-passive-income-pro" ),
    		"not_found" => __( "No Platforms found", "smart-passive-income-pro" ),
    		"not_found_in_trash" => __( "No Platforms found in trash", "smart-passive-income-pro" ),
    		"parent" => __( "Parent Platform:", "smart-passive-income-pro" ),
    		"featured_image" => __( "Featured image for this Platform", "smart-passive-income-pro" ),
    		"set_featured_image" => __( "Set featured image for this Platform", "smart-passive-income-pro" ),
    		"remove_featured_image" => __( "Remove featured image for this Platform", "smart-passive-income-pro" ),
    		"use_featured_image" => __( "Use as featured image for this Platform", "smart-passive-income-pro" ),
    		"archives" => __( "Platform archives", "smart-passive-income-pro" ),
    		"insert_into_item" => __( "Insert into Platform", "smart-passive-income-pro" ),
    		"uploaded_to_this_item" => __( "Upload to this Platform", "smart-passive-income-pro" ),
    		"filter_items_list" => __( "Filter Platforms list", "smart-passive-income-pro" ),
    		"items_list_navigation" => __( "Platforms list navigation", "smart-passive-income-pro" ),
    		"items_list" => __( "Platforms list", "smart-passive-income-pro" ),
    		"attributes" => __( "Platforms attributes", "smart-passive-income-pro" ),
    		"name_admin_bar" => __( "Platform", "smart-passive-income-pro" ),
    		"item_published" => __( "Platform published", "smart-passive-income-pro" ),
    		"item_published_privately" => __( "Platform published privately.", "smart-passive-income-pro" ),
    		"item_reverted_to_draft" => __( "Platform reverted to draft.", "smart-passive-income-pro" ),
    		"item_scheduled" => __( "Platform scheduled", "smart-passive-income-pro" ),
    		"item_updated" => __( "Platform updated.", "smart-passive-income-pro" ),
    		"parent_item_colon" => __( "Parent Platform:", "smart-passive-income-pro" ),
    	];
    
    	$args = [
    		"label" => __( "Platforms", "smart-passive-income-pro" ),
    		"labels" => $labels,
    		"description" => "",
    		"public" => true,
    		"publicly_queryable" => true,
    		"show_ui" => true,
    		"show_in_rest" => true,
    		"rest_base" => "",
    		"rest_controller_class" => "WP_REST_Posts_Controller",
    		"has_archive" => false,
    		"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" => false,
    		"rewrite" => [ "slug" => "platform", "with_front" => true ],
    		"query_var" => true,
    		"menu_icon" => "dashicons-money-alt",
    		"supports" => [ "title", "editor", "thumbnail", "custom-fields", "post-formats" ],
    		"taxonomies" => [ "category", "country", "payout_option", "platform" ],
    		"show_in_graphql" => false,
    	];
    
    	register_post_type( "platform", $args );
    }
    
    add_action( 'init', 'cptui_register_my_cpts_platform' );
    

    Any help would be greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Implications of a custom post type having the same name as a custom taxonomy’ is closed to new replies.