quick question: I'm using a plugin that creates a custom-post-type with the rewrite slug /projects/

So myurl.com/projects/single-project

I want to to rewrite `projects/ with

'rewrite' => array('slug'=>''),

Is there a hook or something so that I can hook to for the register_post_type() function of the external plugin?

I want this rewriting to happen in my functions.php and only if this plugin is activated. I know the name of the custom-post-type "ignition_product"

Kind Regards, Matt


function modify_ign_projects() {
    if ( post_type_exists( 'ignition_product' ) ) {

        global $wp_post_types, $wp_rewrite;
        $wp_post_types['ignition_product']->hierarchical = true;
        $args = $wp_post_types['ignition_product'];
        //print_r( $args ); // [rewrite] => Array ( [slug] => projects
        $wp_rewrite->add_rewrite_tag("%spendenprojekte%", '(.+?)', "projects");
add_action( 'init', 'modify_ign_projects', 100 );
  • 2
    see this question. but note that setting the rewrite slug to an empty value will cause the page post type to 404.
    – Milo
    May 15, 2015 at 15:54
  • Thank you, I updated my question but I'm not properly fond of the use of regular expressions and can't quite figure out how to use add_rewrite_tag(). My sample above those not work, however print_r($args) confirms the slug I want to rewrite. I want to rewrite projects with `spendenprojekte" May 17, 2015 at 7:42

2 Answers 2


I searched everywhere in post type registration flow but didn't found a right way (using filter or action) to do it.

All you can do either register the post type again with a different slug (I prefer) or just run those lines of code which WordPress run to build the rewrite rules in register_post_type.

I register this post type with slug book and changed it using this trick. I am not sure it is 100% correct (as I am rewriting rules for the same post type) but it works.

add_action('registered_post_type', 'testbook', 10, 2);
function testbook($post_type, $args) {
    global $wp_rewrite;
    if ($post_type == 'book') {

        $args->rewrite['slug'] = 'changed_slug_book'; //write your new slug here

        if ( $args->has_archive ) {
                $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
                if ( $args->rewrite['with_front'] )
                        $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
                        $archive_slug = $wp_rewrite->root . $archive_slug;

                add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
                if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
                        $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
                        add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
                        add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
                if ( $args->rewrite['pages'] )
                        add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );

        $permastruct_args = $args->rewrite;
        $permastruct_args['feed'] = $permastruct_args['feeds'];
        add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );

Are you calling flush_rewrite_rules at any moment?

If you are going to change the slug of a custom post type, you also have to call the flush_rewrite_rules.

You can find more documentation here Setting up custom post type archives in WP3.1? Any luck?

And here https://codex.wordpress.org/Function_Reference/flush_rewrite_rules

  • Doesn't make a difference, the code I posted above simply does not work. Also not with flush_rewrite_rules() applied. May 21, 2015 at 6:46

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.