Is it possible to change the URL of custom post types to hide the post type slug?

I would like to turn /{custom-post-type}/{post-name} to /{post-name}, is this possible?

If this is not possible, can I change the labels/terminology in Pages or Posts so that they could be called “Clients” or “Projects” etc?

Related posts

Leave a Reply

4 comments

  1. You need to use option 'rewrite' in your custom post type registration.

    E.g.
    'rewrite' => array('slug' => 'products'),

    From the codex

    When you namespace a URL and still want to use a “clean” URL
    structure, you need to add the “rewrite” element to the array. For
    example, assuming the “ACME Widgets” example from above:

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'acme_product',
          array(
              'labels' => array(
                  'name' => __( 'Products' ),
                  'singular_name' => __( 'Product' )
              ),
              'public' => true,
              'has_archive' => true,
              'rewrite' => array('slug' => 'products')
          )
      );
    }
    

    The above will result in a URL like
    http:/example.com/products/%product_name% (see description of
    %product_name% above.) Note that we used a plural form here which is a
    format that some people prefer because it implies a more logical URL
    for a page that embeds a list of products, i.e.
    http:/example.com/products/.

    Also note using a generic name like “products” here can potentially
    conflict with other plugins or themes that use the same name but most
    people would dislike longer and more obscure URLs like
    http:/example.com/acme_products/foobrozinator and resolving the URL
    conflict between two plugins is easier simply because the URL
    structure is not stored persistently in each post record in the
    database in the same way custom post type names are stored.

  2. 'rewrite' => array('slug' => '/', 'with_front' => false)
    

    This will get the url correctly formed for you, but it will 404 unless you handle it in your theme.

  3. I just changed a slug for a custom post type (in this case, I changed it from "best-practices" to "bestpractices"). I had copied some code that had a blank slug, like so:

    'rewrite' => array('slug' => ''),
    

    Since it was originally left blank, WordPress was just using the name of the custom post type ("best-practices") as the slug. I changed my line of code to

    'rewrite' => array('slug' => 'bestpractices'),
    

    I just wanted to note here that you’ll need to reset your permalinks to get the change to take effect, if you’re changing slugs on pre-existing custom post types.

  4. yes, we can change the slug. Please follow the steps:

    Step 1: Change the slug name from the option “rewrite”. (located at Theme Functions (functions.php))

    ‘rewrite’ => array(‘slug’ => ‘products’)

    Step 2: Reset the permanent link. Otherwise, it will give 404 error.
    1> goto wp dashboard.
    2> Setting –> Permalinks

    Under the common setting section—
    1st change it to plain and save it.
    After that, again change the setting as your preferred format.