Skip Navigation

[Resolved] Pulling custom field from CPT relationship using PHP

This thread is resolved. Here is a description of the problem and solution.

Problem: I am using a PHP script to loop over posts and display information from those posts. I would also like to display custom field information from a related post parent in the loop.

Solution: Use the toolset_get_related_post API to determine the parent post ID, then use that ID in the Types field API types_render_field to display the custom field value from the parent post.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1473601

Hi,

I have a page listing a CPT - all working fine using Views - hidden link

The search at the top uses SearchWP to query these posts and their attached PDFs.

I need the post title on the results page to link to the PDF.

Is there a simple way to pull in a custom field from the related post?

Cheers
Andy

#1473675
Screen Shot 2020-01-22 at 1.23.53 PM.png
Screen Shot 2020-01-22 at 1.23.59 PM.png
Screen Shot 2020-01-22 at 1.24.12 PM.png

You're talking about posts related using Types post relationships, correct? If so, yes you can display custom fields from related posts using Types field shortcodes. To some degree it depends on the type of relationship involved. For example, if the View shows child posts and you want to display a custom field from the parent post, then it's pretty straightforward since there is only one possible parent. When inserting the field using the Fields and Views button, use the "Post Selection" tab to select a related post. You can see the same Post Selection tab by clicking "Edit" next to a field in the Loop Wizard. I'm attaching screenshots of both of those approaches.

If the View shows parent posts and the custom field is in a child post, or in another post related in a many-to-many relationship, then it's not so simple because there can be multiple related posts and you must choose which ones you want to display. You would have to use a View of those related posts with a post relationship filter, then insert the field in that View's loop. Now you would have to nest that View of related posts in the outer View of posts. Let me know if you have questions about this approach.

https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/

#1473787

Hi Christian, unfortunately I’m adding this to a SearchWP template, so not using Views for this part. I’m hoping to use plain ol’ PHP. Cheers, Andy

#1473831

Ah, okay then the most straightforward approach is to use the post relationships API to query related posts first, then use types_render_field to render the field(s) for the related post ID(s) using the "item" attribute. For example, get a related writer post ID and display a custom field from that related post:

$writer_id = toolset_get_related_post( $book_id, array( 'writer', 'book' ) );
echo (types_render_field("my-number", array("style" => "FIELD_NAME : $ FIELD_VALUE", "item" => $writer_id)));
#1474775

Hi Christian,

I've tried to set this up, but it isn't working yet. This is what I have:

foreach ( $swp_query->posts as $post ) {
setup_postdata( $post );
$pdf = toolset_get_related_post( $post, array( "unit-to-unit-combination", "unit" ) );
echo (types_render_field("link-to-pdf", array("item" => $pdf)));

I was also looking at this post https://toolset.com/forums/topic/get-post-parent-witn-new-relationship-model-php/

$post is the parent.
unit-to-unit-combination is the relationship
unit is the child slug
link-to-pdf is the field I want in the child post

Do I have it right? Or am I missing something? I do find the API docs a little hard to follow 🙂

Cheers
Andy

#1475175

Seems okay, but toolset_get_related_post is best when there is only one possible related post to retrieve. In your information, you mentioned that Unit is the child and $post is the parent. Is this a one-to-many relationship? If so, you need to account for the case where there is more than one child of the $post parent, and that means you need toolset_get_related_posts with an "s" at the end. If not, continue.

What are you getting from the toolset_get_related_post API? Add an echo to debug the parent post ID and pdf variable, and let me know what you find out:

echo 'post id: ' . $post->ID;
$pdf = toolset_get_related_post( $post, array( "unit-to-unit-combination", "unit" ) );
echo 'pdf: ' . $pdf;
echo (types_render_field("link-to-pdf", array("item" => $pdf)));
#1476477
2020-01-24 10_03_37-Edit Units to Unit Combinations - Relationships ‹ Laser Awards — WordPress.png

Hi Christian,

Maybe I'm misunderstanding what's a parent and what's a child. The attached shows the relationship. The Units 'belong' to the Unit Combination. So only 1 Unit can be assigned to one Unit Combination (which is where I'm assuming the parent/child relationship).

The output for your code is:

post id: 21143 pdf: 0
post id: 21141 pdf: 0

...and so on for each post

Thanks
Andy

#1479583

In Toolset language, what you have here is a one-to-many (O2M) relationship. O2M relationships are also referred to as parent-child relationships, you may see both names used in the site so be aware they are the same thing. In an O2M relationship, one parent post can be related to many child posts. However, one child post can be related to only one parent. Based on what I see here, in Toolset language Unit is the parent and Unit Combination is the child in this relationship. One Unit can be related to multiple Unit Combination children, but one Unit Combination can only be related to one Unit parent.

The link-to-pdf field is in the Unit post, correct? And $post in your foreach loop is a Unit Combination post, right? If both those statements are true, then you can use toolset_get_related_post to get the related Unit post ID like so:

$pdf = toolset_get_related_post( $post,  "unit-to-unit-combination", "parent" );

Then use that Unit post ID to display the PDF field:

echo (types_render_field("link-to-pdf", array("item" => $pdf)));

Let me know if that works for you.

#1482771

Thanks Christian - all working as expected now! Your help is much appreciated.

Andy

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.