Skip Navigation

[Resolved] Calling Parent field in Loop but not views loop

This support ticket is created 6 years, 8 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)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#626639

I have a post type called properties and a post type called special offers. Properties is the parent of special offers.

I am trying to create a special offers slider that shows only the special offers of the parent in the loop, which I can do without any issue.
However the plugin I am using is called slick slider and it cannot get any fields from the parent of the post in the loop.

It works if I put the post id but I need a variable as although I could make 13 separate sliders this would not enable me to display all the results as one slider on the homepage.

I have tried

[types field="guests" id="$property"][/types]
{!{types field='guests' id='$property'}!}{!{/types}!}
[types field="guests" id="41340"][/types] (this works).

I have read

https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

I realise I could do this with a view but I am finding that calling the views is much slower than using this slider on the page. 0.5 seconds difference.

Here is an example at the bottom of the page, under the title special offers

hidden link

I am using a child theme and so I am happy to try and use a function.

I was wondering if it would be able to call the field "child of" and then add that to a shortcode that said

[types field="guests" id="$wpcf-child-of"][/types]

Or even using jquery.

I realise that you cannot offer customisations and so if you could point me in the direction of what is possible that would be great.

#626778

Hi, Types stores a reference to the parent post's ID in the child's postmeta as a hidden field in the format "_wpcf_belongs_" + parent post type slug + "_id". So if your parent post type slug is "property", there is a hidden postmeta field on the child post called "_wpcf_belongs_property_id" that contains the parent property post's ID.

If you want to access that parent's ID, you could call get_post_meta on the child post:

$parent_id = get_post_meta( 1234, '_wpcf_belongs_property_id', true );

Replace 1234 with the numeric ID of the child post, or a variable that represents that numeric ID. If you create a custom shortcode that returns this $parent_id value, you could insert that shortcode in the "id" attribute of the Types field shortcode to explicitly define the parent property ID. Be sure to add your custom shortcode name in Toolset > Settings > Frontend content > Third party shortcode arguments.

More information about custom shortcodes:
https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

#627588

Hi,

Thank you for your reply. The problem is that I am trying to use 1 slide layout, like a content template in a slider loop to call every post, I do not have the option to make a slide for each post

So I cannot specify 1 post id, I need to specify get_post_meta of post in loop and display custom field of post in the loop.

I am displaying the Featured images and post title without any issues but I cannot get the custom fields to display.

I look forward to your response.

Thank you

Matt

#628065

So I cannot specify 1 post id, I need to specify get_post_meta of post in loop and display custom field of post in the loop.
Right, you need a variable that represents the current post ID. I'm not sure how your slider plugin works. Inside the "template" loop, can you access to the current special offer ID using a Views shortcode? For example, if you place a shortcode like [wpv-post-id] in the template, do you see the ID of each special offer appear? If so, then you can use the same Views shortcode in a custom shortcode attribute like this:

[your-custom-shortcode offer="[wpv-post-id]"]

Then in your custom shortcode callback function, you can access that offer ID from the attributes, and use it in your PHP code to determine the parent property. Then use get_post_meta to access the parent property's custom fields.
https://codex.wordpress.org/Shortcode_API#Handling_Attributes