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?