Custom post types permalink with parent custom post type

It is difficult to determine the title of this question ....

I want to create a nice readable constant reference structure for my 2 custom post types (CPT). My first CPT "family" has the following correspondence " family /% postname% " (everything works fine)

The second CPT "childs" has a meta box where I can select the parent field by selecting the CPT "family" to which the child CPT belongs. This also works great.

Now I have set the rewrite-slug for "childs" to " % parent_post_url% / child /% postname% " to get the next url " family / the-griffon / child / peter . But when I call this url wordpress does not render found page. The crazy thing is that if I set the rewrite to slug " family / the-griffons / child /% postname% " I can call the url (both urls are the same !!!)

So why does WP pads throw an error when I try to get the url dynamically but not when I hardcode the url?

+3


source to share


1 answer


The parent-parent relationship you think you have is not quite there. Based on what you've told us so far, it seems that all you have is a custom field denoting a "pseudo-parent" id. So your question should really read:

How do I rewrite the first part of the cpt url based on the value of the cpt custom field?

because, as far as Wordpress is in your case, whatever the "parent id" is really is the value of the custom field.

you can try the last part (part 3.) of this tutorial , bearing in mind that you will need the actual path to the url of the "parent id" and not the value of the "parent id" cf itself, you will need to implement something by lines:



$parent_id = get_post_meta($post_id, "your_cf_key_for_parent_id", true);
$full_parent_post_url =  parse_url( get_permalink( $parent_id ) );
$parent_post_url =  $full_parent_post_url['path'];
if(!$parent_post_url) { $parent_post_url = "default-fallback" }
$permalink = str_replace(‘%parent_post_url%’, $parent_post_url, $permalink);
return $permalink;

      

another relevant answer on stackexchange:

using-custom-fields-in-custom-post-type-url

BUT as @ c0ns0l3 mentioned using custom taxonomies is the correct way to do this.

+1


source







All Articles