Wordpress - Change Custom Post Type slug
The register_post_type_args
filter can be used to modify post type arguments:
add_filter( 'register_post_type_args', 'wpse247328_register_post_type_args', 10, 2 );
function wpse247328_register_post_type_args( $args, $post_type ) {
if ( 'portfolio' === $post_type ) {
$args['rewrite']['slug'] = 'stories';
}
return $args;
}
@dave-romsey answer didn't work for me, PHP kept telling me Warning: Cannot use a scalar value as an array in /path/to/functions.php
So I went the array_merge
way.
Complete function you need to add to your child's theme functions.php
file:
function update_portfolios_slug( $args, $post_type ) {
if ( 'portfolios' === $post_type ) {
//$args['rewrite']['slug'] = 'presidentes';
$my_args = array(
'rewrite' => array( 'slug' => 'presidentes', 'with_front' => false )
);
return array_merge( $args, $my_args );
}
return $args;
}
add_filter( 'register_post_type_args', 'update_portfolios_slug', 10, 2 );
After uploading your code don't forget to "refresh" your Permalinks by going to Settings > Permalinks and hitting Save changes.
Cheers.