Change custom post type url

14,262

Solution 1

I am presuming you have added the custom post type yourself, in which case it is easy.

When you register a new post type, you can set any rewrite rules for that post type to follow as part of the arguments used in the register_post_type( 'name', $args ) function.

If your post type is available on the front end and is public, then by default WordPress will use the custom post name as the slug. You can override this as follows:

$args = array(
    // your other arguments
    'rewrite' => array( 
        'slug' => 'products', // use this slug instead of post type name
        'with_front' => FALSE // if you have a permalink base such as /blog/ then setting this to false ensures your custom post type permalink structure will be /products/ instead of /blog/products/
    ),
);

register_post_type( 'portfolio', $args );

The other arguments you can use are documented at http://codex.wordpress.org/Function_Reference/register_post_type

Solution 2

Please see the documentation on URLs of Namespaced Custom Post Types Identifiers. One of the options available to you is to add a 'rewrite' argument to register_post_type(). From the docs:

When you namespace a custom post type identifier and still want to use a clean URL structure, you need to set the rewrite argument of the register_post_type() function. For example:

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

You'll then likely need to login to the admin and re-save your permalinks.

Solution 3

Use the rewrite property

$args= array(
    // other settings
    'rewrite' => array( 'slug' => 'Products' ),
);
register_post_type( 'portfolio', $args);
Share:
14,262
Shoeb Mirza
Author by

Shoeb Mirza

Hi there, You can reach me: Skype: shoaib_mirza80 Gmail: [email protected] Latest Technologies I'm working on: NodeJS for frontend and VueJS frontend

Updated on June 05, 2022

Comments

  • Shoeb Mirza
    Shoeb Mirza almost 2 years

    I've a custom post type by name 'Portfolio'. It's generating the url as /portfolio/xxx when I make any post. I want to change it to 'Products'. Change custom post-type rewrite I've tried this but it doesn't suppose to work.

  • rnevius
    rnevius about 9 years
    It does work. In fact, it's the only way it should work. We need to see your code in order to help you.
  • Shoeb Mirza
    Shoeb Mirza about 9 years
    I've posted your code in functions.php and it is not working. You need to understand this.
  • rnevius
    rnevius about 9 years
    Of course my code doesn't work. It's not a drop-in piece of code. You need to modify your OWN code.
  • Shoeb Mirza
    Shoeb Mirza about 9 years
    Dude, I'm not expertise in deep coding of php. That's why I posted here to ask how to achieve that..