Change custom post type to hierarchical after being registered

Very similar to this previous question: Changing ‘rewrite’ argument after custom post type is registered

I am trying to make MarketPress Products hierarchical – I can do it with hacking the plugin files, but I would like to stay away from them if I can.

Is it possible to change arguments of a custom post type after it has been registered, but before all the internal rewrite stuff has been done?

UPDATE: Here’s the solution

And as it usually happens, I find the answer a few minutes after posting the question…

So here’s what I did in my theme’s functions.php file to solve my problem:

function modify_products() {
    if ( post_type_exists( 'product' ) ) {

        /* Give products hierarchy (for house plans) */
        global $wp_post_types, $wp_rewrite;
        $wp_post_types['product']->hierarchical = true;
        $args = $wp_post_types['product'];
        $wp_rewrite->add_rewrite_tag("%product%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=product&name=");
        add_post_type_support('product','page-attributes');
    }
}
add_action( 'init', 'modify_products', 1 );

Everything works: hierarchy, rewriting, etc 🙂

1
1

And as it usually happens, I find the answer a few minutes after posting the question…

So here’s what I did in my theme’s functions.php file to solve my problem:

function modify_products() {
    if ( post_type_exists( 'product' ) ) {

        /* Give products hierarchy (for house plans) */
        global $wp_post_types, $wp_rewrite;
        $wp_post_types['product']->hierarchical = true;
        $args = $wp_post_types['product'];
        $wp_rewrite->add_rewrite_tag("%product%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=product&name=");
        add_post_type_support('product','page-attributes');
    }
}
add_action( 'init', 'modify_products', 1 );

Everything works: hierarchy, rewriting, etc 🙂

Leave a Comment