How can I place a meta value in rewrite rule for c

2019-02-23 02:01发布

问题:

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 (procedure) that features a custom meta key/value with a page ID that I want to use as the slug.

I'm using this function (below) to create the permalinks in the admin area, but when viewing these, the pages are 404 errors. How can I create rewrite rules to use this same format?

function bv_procedure_parent_slug($url, $post) {
    if(get_post_type($post) == 'procedure' && get_post_meta($post->ID, 'procedure_parent', true)) {
        $procedure_parent = get_post(get_post_meta($post->ID, 'procedure_parent', true))->post_name;
        if($procedure_parent) {
            $url = str_replace('procedure', $procedure_parent, $url);
        }
    }
    return $url;
}
add_filter('post_type_link', 'bv_procedure_parent_slug', 1, 3);

The goal here is that I'll have lots of posts here, that will contain a meta key/value of procedure_parent => 31 (where 31 is a page ID, and the post name is 'face'). When viewing the single post, rather than the URL being /procedure/facelift/ I would like it to be /face/facelift/.

For this, I believe I need to be able to get access to $post when creating the rewrite rule so I can get use get_post_meta().

But how?

回答1:

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!