Skip Navigation

[Closed] Coding Post Relationship (Parent of Child)

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 11 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

This topic contains 10 replies, has 3 voices.

Last updated by Maresca 11 years, 10 months ago.

Assisted by: Srdjan.

Author
Posts
#10755

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.

#10770

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);

#10812

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?

#11013

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.

#11285

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.

#11297

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.

#11299

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

#11300

Unfortunately, same result. I thought for sure you had it there. I appreciate the help though.

#11302

Can you echo $cptb_parent_id to see what value it is.

#11303

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.

#11305

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!

The topic ‘[Closed] Coding Post Relationship (Parent of Child)’ is closed to new replies.