Hi, I have a setup where CPT C serves as an intermediary between CPT A and CPT B, as in:
https://toolset.com/faq/how-do-i-associate-one-child-with-several-parents-of-the-same-type
So the intermediary custom post type is a child of both CPT A and B.
r
I'm setting up a template (using PHP) for CPT A and figured out how to render the child fields for CPT C (the intermediary). However, I also want to display the Post Title for CPT B, which is the second parent of the intermediary and the main data for this relationship. How do I do that using php?
I saw that there are some functions like wpcf_pr_get_has which deal with post-relationships but I'm not sure how to write it up to get what I want.
Hi Maresca,
first get ID of second parent (CPT B) with call:
$cptb_parent_id = wpcf_pr_post_get_belongs($post_id, 'CPT B');
(replace $post_id with CPT C post ID, and 'CPT B' with CPT B post type slug)
Then you can fetch post title or something else.
echo get_the_title($cptb_parent_id);
Getting close but I have multiple CPT C post IDs for each CPT A page. It works when I enter the actual CPT C post ID in the template, but how do I get each CPT C ID with PHP?
Still can't get it. I tried this:
$child_posts = types_child_posts('my-cptb-slug');
$child_posts_id = get_the_id($child_posts);
$cptb_parent_id = wpcf_pr_post_get_belongs($child_posts_id, 'my-cptb-slug');
echo get_the_title($cptb_parent_id);
Just displays the title of the current post.
Hi Maresca,
get_the_id is just going to return the current post id - http://codex.wordpress.org/Function_Reference/get_the_ID
I think you need something like:
$child_posts = types_child_posts('my-cptc-slug');
foreach ($child_posts as $child_post) {
$cpta_parent_id = wpcf_pr_post_get_belongs($child_post, 'my-cpta-slug');
echo get_the_title($cpta_parent_id);
}
This assumes that the child posts are my-cptc-slug and the parent is my-cpta-slug
Best regards,
Bruce.
Still not working. It just displays the current page post title. Here is the scenario to be clearer:
CPTA is the current page (template) and parent of CPTB.
CPTC is the child post of the current page and intermediary between CPTA and CPTB.
CPTB is the second parent of CPTC.
I'm trying to display the title of CPTB on the CPTA page template.
I have the following:
$child_posts = types_child_posts('cptc-slug');
foreach ($child_posts as $child_post) {
$cptb_parent_id = wpcf_pr_post_get_belongs($child_post, 'cptb-slug');
echo $child_post->fields['cptc-field-a'];
echo ' ';
echo $child_post->fields['cptc-field-b'];
echo get_the_title($cptb_parent_id);
The CPTC fields display properly but the CPTA title displays rather than the CPTB title.
(Please also note that there will be multiple CPTA titles in the future).
Thanks.
Hi Maresca,
Try changing
$cptb_parent_id = wpcf_pr_post_get_belongs($child_post, 'cptb-slug');
to
$cptb_parent_id = wpcf_pr_post_get_belongs($child_post->ID, 'cptb-slug');
Best regards,
Bruce
Unfortunately, same result. I thought for sure you had it there. I appreciate the help though.
Can you echo $cptb_parent_id to see what value it is.
Right now, nothing. But I didn't realize that I also lost the cptc field values, which were working so I have to go back to one of my previous attempts. I'll post that as it was very close.
We have a winner! In the last attempt, I mistyped one of the slugs, so what works is:
$cptb_parent_id = wpcf_pr_post_get_belongs($child_post->ID, 'cptb-slug');
Thank you so much, Bruce, for your help!