9

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 :)

3
  • 5
    Ragulka, can you remove the SOLVED from your title and post your solution as an answer. After 24 hours you will be able to accept it. It just makes it easier for people who may have the same / similar problem. Thanks. Commented Jan 27, 2012 at 12:54
  • @ragulka welcome to WordPress Stack Exchange! Please note: this site is not the usual support-forum format, but rather uses a question and answer format, in order to build a robust knowledge base. Questions and answers are posted separately, and are up- and down-voted by the community. The OP "accepts" the answer that best-resolved the original question. So, as Stephen Harris said: please post your solution as an Answer, and then accept that answer. Commented Jan 27, 2012 at 15:34
  • Hey, sorry about that. I actually tried to answer my own question but I couldn't, I was told to wait either 8 hours or add a comment or edit my question instead. So I did. Well, I guess I'll wait another 4 hours and then I can answer my question.
    – ragulka
    Commented Jan 27, 2012 at 16:29

1 Answer 1

10

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 :)

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.