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 🙂

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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 🙂


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x