WordPress.org

Ready to get started?Download WordPress

Forums

Replacing slug in CPT (4 posts)

  1. webg
    Member
    Posted 1 year ago #

    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

  2. bcworkz
    Member
    Posted 1 year ago #

    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

  3. webg
    Member
    Posted 1 year ago #

    Thanks

  4. c_cav
    Member
    Posted 1 year ago #

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

    See this post

Topic Closed

This topic has been closed to new replies.

About this Topic