How can I place a meta value in rewrite rule for custom post type?

前端 未结 1 1523
慢半拍i
慢半拍i 2021-01-19 02:38

I\'ve completely re-written this question as it got a bit long, and I was worried people skipped it without reading it completely.

I have a custom post type (procedu

1条回答
  •  北海茫月
    2021-01-19 03:07

    It seems like you've over complicated your problem a little. Would it work if you did the following?

    • Install the Custom Post Type Permalinks plugin and
    • Change the default permalink to http://example.com/custompostname/%category%/%postname%/.
    • Then instead of creating some pages with categories with them, use WordPress' inbuilt archive functionality and make a custom archive template for your custom post.

    Otherwise I think add_permastruct() is the function you're missing. I'm theorising this looking at the solution at https://wordpress.stackexchange.com/a/253325/58884 for a similar problem. Some code like this:

    function my_custom_rewrite() {
        add_permastruct('procedure', '/%parent%/', false);
    }
    add_action('init', 'my_custom_rewrite'); 
    

    rather than 'rewrite' => array( 'slug' => '%parent%', 'with_front' => false ) in the post type declaration. The 'slug' => '%parent%' part of that declaration is an optional substitution for the name of the custom post type ($post_type in the register_post_type() codex entry). So if you had 'rewrite' => array( 'slug' => 'wibblewobble', 'with_front' => true) your url would change from http://example.com/procedure/%postname%/ to http://example.com/wibblewobble/%postname%/. With 'with_front' => false I don't think it does anything. When you've entered 'slug' => '%parent%' I think WordPress is taking that as a string and not doing anything with it dynamically.

    Also be sure to use flush_rewrite_rules();. You can use it in the init hooked function while testing and then move it to theme/plugin activation hooked function once it works.

    Good luck!

    0 讨论(0)
提交回复
热议问题