Skip Navigation

[Resolved] Display the custom field from last child

This support ticket is created 7 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by Beda 7 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#423325

I have a content template that displays "types fields" information from my Parent Custom Post Type. The Parent has many children.

I want to show one of the custom fields from the CHILD post in the content template, but only from the child post that was submitted last.

So - I want the content template to show the last value submitted of a custom field from the last child submitted.

I am sure there are a few ways this could be done. Can you help me with the best approach to this?

One way I could think of is to create another field for the parent post field group and update this value with "add_action ('cred_save_data') in my functions file once the Child Post CRED is submitted.

But it seems like there could be a better way to add a shortcode in this content template and not have so much redundant data.

Is it possible to display the custom field "my-custom-field" from the last child post in this content template?

For example:
[types field="my-custom-field" id="$child-post-type-slug"][/types]

Is there a better way to do this what a custom function that could retrieve the value of that custom field from a specific post type and then just display this with a shortcode?

I looked at the documentation here:

https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

My last though was to create a VIEW that only returns that custom field, and just add that specific VIEW to the content template.

Do you have a suggestion of which method would be best. I don't want to bog down the site performance if one way is better than another.

#423371

In Toolset you can display Parent Fields by calling a $parent ID in your ShortCode, I assume you do that already as you successfully display Parent Fields.
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/
https://toolset.com/documentation/user-guides/displaying-fields-grandparents/

To display Fields of Child Posts, you can not follow the same flow.
Here is how you will display Child Post Data:
https://toolset.com/documentation/user-guides/display-fields-table-content-using-views-or-the-types-api/

To display only Data of the Last Post (Child Post) Submitted you could use the Order By (Post date) and then limit the View's Output (limit and offset) to 1.

This would give you one, the last, post submitted.

So, you need a View, as described in above Link, and insert this View to your Parent Post Type Content Template.

#423881

Beda thanks,

I can't seem to get this to work correctly. Based on your suggestion:

1. Created a new VIEW
2. Choose the POST type I would like to display the info from
3. Order by post date with limit and offset as "1"

Then add my loop output to retrieve the custom field from this post type that I would like to display:

[wpv-layout-start]
    [wpv-items-found]
    <!-- wpv-loop-start -->
    <wpv-loop>
      [types field="inspection-results"][/types]
    </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
[wpv-layout-end]
	

I then add this new VIEW to my content template, but it returns the same value for each item. What appears to be happening at the moment is that the new VIEW is only showing the last value from the last child submitted and not from each child of it's parent.

In my current usage the content template is displaying all of the parent posts. As an example, if this was a real estate site, the content template would be displaying all of the properties.

If a child post type called "property details" contained all of the property info and one of those fields was called "occupied status", we would simply want to show the last value entered of that field on the front end for each property so that each post loaded would show the property along with the last value entered in "occupied status". (which would be in the property's last child)

If the new VIEW I created worked correctly, when the content template loads all of the parent posts, each parent post should show on it, the last value entered for it's child post's custom field.

Could help me solve this?

#424501

1. I then add this new VIEW to my content template, but it returns the same value for each item.

Yes, this is because with the Limit/Offset set to one, it will always return the first post in the Query, which order-by is determined by the order settings of your View.

Can you try to add a Query Filter to your View.

Select posts that are children of the Post where this View is shown.

This will return only the Posts that are Child to the current Post (displayed with the Content template)

Make sure the Order by settings is set to Post Date.

Now, what happens shoould be this:

- The View retunrs ALL Child Posts of your Current Post
- Orders them by Date (post date, newest first)
- The Limit/Offset makes sure only the first is rendered.

Does this work?

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