Search code examples
phpwordpresscustom-wordpress-pagespermalinks

Set custom post type permalink to root in WordPress


I created a custom post type to organize a certain type of pages, but since my WordPress permalinks settings are set to /blog/%postname%/ this new custom post type permalink is /blog/new-post-type. I do not want to remove /blog/ from the site's permalink settings because we do use that for blog posts. How do I remove /blog/ from the new post type so it acts like the default pages permalinks in WordPress which is www.website.com/name-of-page/?


Solution

  • The rewrite parameter of the register_post_type() function has a parameter of with_front that when set to false will not prepend URLs with the front base specified by the permalink structure.

    register_post_type( 'cpt', array(
        'rewrite' => array(
            'with_front' => false,
        )
    );
    

    The above will prevent /blog/ from being on URLs for your CPT's posts, however, I do not know if that means the CPT's URLs will work at root.