Change custom post type to hierarchical after being registered

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