WordPress.org

Support

Support » Plugins and Hacks » Hacks » Replacing slug in CPT

Replacing slug in CPT

  • I’m using Workality theme for one of my projects, but to fit specifics of this project I need to change the defined custom post type permalink (slug), so changed the code:

    register_post_type( 'works' , array(
    			'label'=>_x('Works','Type','dronetv'),
    			'description'=>__('Special type of post for creating project','dronetv'),
    			'labels' => $labels,
    			'public' => true,
    			'menu_position' => 5,
    			'show_ui' => true,
    			'show_in_menu' => true,
    			'publicly_queryable' => true,
    			'exclude_from_search' => true,
    			'query_var' => true,
    			'menu_icon'=>get_template_directory_uri() . '/images/portfolio_icon.png',
    			'rewrite' => true,
    			'capability_type' => 'post',
    			'hierarchical' => false,
    			'supports' => array( 'title', 'editor','thumbnail')
    			)
    		  );

    to this one:

    register_post_type( 'works' , array(
    			'label'=>_x('Works','Type','dronetv'),
    			'description'=>__('Special type of post for creating project','dronetv'),
    			'labels' => $labels,
    			'public' => true,
    			'menu_position' => 5,
    			'show_ui' => true,
    			'show_in_menu' => true,
    			'publicly_queryable' => true,
    			'exclude_from_search' => true,
    			'query_var' => true,
    			'menu_icon'=>get_template_directory_uri() . '/images/portfolio_icon.png',
    			'rewrite' => array('slug' => 'themes'),
    			'capability_type' => 'post',
    			'hierarchical' => false,
    			'supports' => array( 'title', 'editor','thumbnail')
    			)
    		  );

    Now it works fine for me, my permalink was changed from “works” to “themes”, but I’n not sure if the changes I made is correct solution, I’m not code familiar, so please help me if I did something wrong?

    If you have better solution for changing themes existing CPT slug, please share it with us.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • The problem with doing it that way is when you update the theme, your changes will be lost. You should modify theme constructs from a child theme. There should be no problem in theory updating an existing CPT by simply redefining it again with register_post_type(), but from a child theme, it may need to be done from a proper action hook, depending on which order the functions.php files are processed.

    You will likely also need to flush the rewrite cache once by switching permalink styles back and forth to get the change to ‘stick’. Check out the Codex entry for more info: Function_Reference/register_post_type

    Thanks

    I just posted instructions on how to modify a custom post type.

    See this post

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Replacing slug in CPT’ is closed to new replies.