0

I want to rewrite custom post type slug. I used this snippet. but not working.

add_filter( 'register_post_type_args', 'wpse247328_register_post_type_args', 10, 2 );
function wpse247328_register_post_type_args( $args ) {

    if ( 'stores' === $post_type ) {
        $args['rewrite']['slug'] = 'location';
    }

    return $args;
}
4
  • Could you elaborate on "not working" ? What do you expect to happen? What happens instead? Did you flush rewrite rules and such?
    – Rarst
    Sep 14, 2017 at 16:46
  • Url slug not updating .. old url was: www.abc.ocm//stores/mo-ellisville/ after add this code url not updating . :( Sep 14, 2017 at 16:51
  • 1
    You may need to use unregister_post_type then re-register it, then visit the Settings > Permalinks page to flush rewrite rules.
    – WebElaine
    Sep 14, 2017 at 19:09
  • Did you flush rewrite rules after changing the slug? unregister_post_type was only necessary before the register_post_type_args filter was introduced in v4.4.
    – Milo
    Sep 15, 2017 at 3:15

3 Answers 3

0

I had to face a similar issue a while ago and got it resolved by following this guide.

Basically what you have to do is two functions 1) Custom WordPress Rewrite Rule and 2) Custom Post Type Link.

The Rewrite Rule function will make sure the custom slug for your CPT is handled properly throughout the site. And the second function will ensure that it displays the URL the way you want - you can also include taxonomy variables if you wish to. Anyway take a look at the article as it helped me a lot and I hope it helps you too as well.

0

Just at the rewrite slug action in your custom post type code?

$rewrite = array(
        'slug'                  => 'Your-new-slug',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
    );
0

According to the source code, it's obviously because of missing argument https://developer.wordpress.org/reference/hooks/register_post_type_args/ Just add $post_type as second param to your function

add_filter( 'register_post_type_args', 'wpse247328_register_post_type_args', 10, 2 );
function wpse247328_register_post_type_args( $args, $post_type ) {
    if ( 'stores' === $post_type ) {
        $args['rewrite']['slug'] = 'location';
    }
    return $args;
}

One more thing, please set WP_DEBUG to true while working locally to see all the warnings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.