0
votes

I have a custom post type named datacenter_news and a taxonomy for it named dc_news_tax. Both the post type archive and taxonomy archive works as it should. But when i try to add a custom rewrite slug named datacenter-news to the post type the URLs to my taxonomy terms stops working and give me the 404 page.

Right now the permalink structure looks like this.

datacenter_news/dc_news_tax/term

and here is how i want it to look like

datacenter-news/dc_news_tax/term

Here is the registered post type.

function cptui_register_my_cpts_datacenter_news() {

    $labels = array(
        "name" => __( "Data Center News", "custom-post-type-ui" ),
        "singular_name" => __( "Data Center News", "custom-post-type-ui" ),
    );

    $args = array(
        "label" => __( "Data Center News", "custom-post-type-ui" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => true,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "datacenter-news", "with_front" => false ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail" ),
        "taxonomies" => array( "news_tag", "dc_news_tax" ),
    );

    register_post_type( "datacenter_news", $args );
}

add_action( 'init', 'cptui_register_my_cpts_datacenter_news' );

and here is the registered taxonomy

function cptui_register_my_taxes_dc_news_tax() {

    $labels = array(
        "name" => __( "News Category", "custom-post-type-ui" ),
        "singular_name" => __( "News Category", "custom-post-type-ui" ),
    );

    $args = array(
        "label" => __( "News Category", "custom-post-type-ui" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'dc_news_tax', 'with_front' => true,  'hierarchical' => true, ),
        "show_admin_column" => false,
        "show_in_rest" => true,
        "rest_base" => "dc_news_tax",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
        );
    register_taxonomy( "dc_news_tax", array( "datacenter_news" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_dc_news_tax' );

My permalink settings for the post type looks like this /%dc_news_tax%/%postname%/

What do i need to change to get custom rewrite slug to work with the URLs to my taxonomy terms?

Thanks in advance.

1

1 Answers

0
votes

There are a few things that could be causing this. I'd refer to the the documentation in regards to creating custom post types. First i'd try changing this in your code. Then refresh permalinks

https://www.smashingmagazine.com/2012/11/complete-guide-custom-post-types/

From

register_post_type( "datacenter_news", $args );

To

register_post_type( "datacenter-news", $args );

From

register_taxonomy( "dc_news_tax", array( "datacenter_news" ), $args )

To

register_taxonomy( "dc_news_tax", array( "datacenter-news" ), $args )