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

白昼怎懂夜的黑 提交于 2019-12-01 18:53:59

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!