0

I have a site with around a dozen custom post types. I'd like to change the default post type so that it's URL has a slug of /news/.

In my functions file I have:

    add_action( 'init', 'change_post_object' );
    // Change dashboard Posts to News
    function change_post_object() {
        $get_post_type = get_post_type_object('post');
        $labels = $get_post_type->labels;
        $labels->name = 'News';
        $labels->singular_name = 'News';
        $labels->add_new = 'Add News Item';
        $labels->add_new_item = 'Add News Item';
        $labels->edit_item = 'Edit News';
        $labels->new_item = 'News';
        $labels->view_item = 'View News Item';
        $labels->search_items = 'Search News';
        $labels->not_found = 'No News found';
        $labels->not_found_in_trash = 'No News found in Trash';
        $labels->all_items = 'All News';
        $labels->menu_name = 'News';
        $labels->name_admin_bar = 'News';
        $rewrite = $get_post_type->rewrite;
        $rewrite->slug = 'news-events/news';
        $rewrite->with_front = 'true';
    }

... Which all works apart from the rewrite of the slug.

Lots of posts suggest just changing the permalink structure on the Permalinks page from /%postname%/ to /news/%postname%/. This works - but breaks all the other custom post types.

How can I do this please?!

1 Answer 1

1

Change the Permalinks structure as you mentioned, but also add (or change) the following line for all the other CPTs:

$rewrite->with_front = 'false';

Setting with_front to false will strip off whatever prefix you have listed in your Permalink settings.

If setting that up and hitting the Permalinks page to flush rewrite rules doesn't immediately fix the problem, you might need to call unregister_post_type() for each existing CPT before re-registering them, as they can get cached pretty hard in some cases.

1
  • Brilliant - thanks
    – JohnG
    Dec 6, 2021 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.