Skip Navigation

[Resolved] Managing display of parent custom fields and RFG in child’s Content Template

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

Problem: I have a one-to-many relationship between Organizzate (parent) and Consuntivi (child). I have several custom fields on the Organizzate post, including a repeatable field group (RFG). On the Consuntivi post Content Template, I would like to display the custom fields from the parent Organizzate post, as well as a View of RFG from the parent Organizzate post.

Solution: To display custom fields of the parent post, you should use the Types field syntax like this:

[wpv-conditional if="( '[types field="patrocinio" separator=", " item="@iniziativa-consuntivo-iniziativa.parent" output="raw"][/types]' eq '' )"]
       <span>Nessun patrocinio concesso</span>
   [/wpv-conditional]
   [wpv-conditional if="( '[types field="patrocinio" separator=", " item="@iniziativa-consuntivo-iniziativa.parent" output="raw"][/types]' ne '' )"]
       [types field='patrocinio' separator=', ' item='@iniziativa-consuntivo-iniziativa.parent'][/types]
   [/wpv-conditional]   

To display a View of RFG from the parent post, set up the View's post relationship filter to respond to a shortcode attribute "wpvrelatedto". Then use the wpv-post-id shortcode to set the value of that shortcode attribute, like this:

[wpv-view name="Your RFG View" wpvrelatedto="[wpv-post-id item='@relationship-slug.parent']"]

Relevant Documentation:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

This support ticket is created 5 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)

This topic contains 5 replies, has 2 voices.

Last updated by Silvia 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1195951

I am building a website that keeps track of all the activity in my department. For every successfully planned event I want to create a final report, with infos about the event itself and some additional information about the outcome in terms of people taking part to it, new business contacts, new associates.
I built a CPT called Iniziative Organizzate (in English “planned events”) that has, among other custom fields, a repeatable field groups that stores budget entries, with fields item, company, amount and +/-.
I also created another CPT called Consuntivi (Final reports) that has a relationship of 1 to 1 with Planned Events (which is parent).

I would like to build a custom template for the final report that, besides listing the custom fields belonging to the REPORT cpt, would list all the custom fields, including the RFG with all the budget entries, of the parent EVENT CPT.

While it’s not a problem to list parent fields adding the “item='@relationship-slug.parent” code (EXCEPT for the problem described below), I don’t know how to properly filter the view that lists all the RFG fields in order to be displayed in the child custom template.

In addition (I don’t know if I have to open another ticket for this problem) I was building a test on certain parent fields to display different text if field is empty or field value IF field is not empty with this syntax :

 [wpv-conditional if="( $(wpcf-patrocinio).id(iniziativa) eq '' )"]
    	<span>Nessun patrocinio concesso</span>
    [/wpv-conditional]
    [wpv-conditional if="( $(wpcf-patrocinio).id(iniziativa) ne '' )"]
 		[types field='patrocinio' separator=', ' item='@iniziativa-consuntivo-iniziativa.parent'][/types]
    [/wpv-conditional]   

(Patrocinio is a custom field in Iniziativa parent cpt and is being displayed in Consuntivo Iniziativa child cpt content template) but regardless of the field value, it always shows alternative text, as if the field was empty. What am I doing wrong?
Thank you as always for your precious help!

#1196256

Hi, unfortunately the $(slug).id syntax has some problems in conditionals (see https://toolset.com/errata/wpv-conditionals-populated-with-types-fields-not-from-current-post-as-at-a-origin-will-fall-back-to-the-current-post-data/). We are working to fix that. Until it's fixed, the workaround is to use the Types field shortcode notation instead. Try this:

[wpv-conditional if="( '[types field="patrocinio" separator=", " item="@iniziativa-consuntivo-iniziativa.parent" output="raw"][/types]' eq '' )"]
       <span>Nessun patrocinio concesso</span>
   [/wpv-conditional]
   [wpv-conditional if="( '[types field="patrocinio" separator=", " item="@iniziativa-consuntivo-iniziativa.parent" output="raw"][/types]' ne '' )"]
       [types field='patrocinio' separator=', ' item='@iniziativa-consuntivo-iniziativa.parent'][/types]
   [/wpv-conditional]   

Let me know if the problem is not resolved by this workaround.

#1196326

Thanks Chris, I will try that and let u know if it works. Can you please advise on my first question as well? Thank you very much!

#1196336

Chris, I tried the solution you suggested for the second part of my ticket and I want to confirm you that it works perfectly!
Now I only need help about displaying parent rfg on child's content template as specified in the first part of this ticket. Thank you SO much!!!

#1196396
Screen Shot 2019-02-05 at 6.00.19 PM.png

Okay sure, you'll create a new View. In the popup dialog, choose "full custom display" to show all the editor panels. In Content selection, choose the RFG. In the Query Filter panel, add a "Post relationship or repeatable field group owner" filter and choose the option "post with ID set by the shortcode attribute wpvrelatedto" (see attachment).
This means that you want to show all the RFGs associated with a specific post, and you will be providing that parent post ID in the View shortcode as an attribute, like this:

[wpv-view name="Your RFG View" wpvrelatedto="12345"]

This technique is called "passing arguments to Views", and we have more information about that if you're interested: https://toolset.com/documentation/user-guides/passing-arguments-to-views/

Next, you need to change that 12345 to be dynamic, which means the parent post ID can be different depending on where you show the View. To get the parent post ID on a child post template, you can use a post ID shortcode. In the child post template, click "Fields and Views" and click "Post ID" to insert a post ID shortcode. In the configuration popup, click "Post selection" on the left side and choose "A post related to the current post, set by a Types relationship". Then pick the parent post type in the relationship that corresponds to your parent / child posts. The shortcode produced by the system will look like this:

[wpv-post-id item='@relationship-slug.parent']

Finally you will combine the View shortcode and the dynamic parent post ID shortcode to show all the RFGs for the parent post, and place that View shortcode in your child post template:

[wpv-view name="Your RFG View" wpvrelatedto="[wpv-post-id item='@relationship-slug.parent']"]
#1196530

I don't know how to thank you, it was fantastically explained and worked like a charm. You didn't only provide a solution but you explained how to get there, and I appreciate it more than you can imagine. Thank you so much!!!

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