[Closed] Custom field inheritance and count

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.

Author
Posts
#1613325

Tell us what you are trying to do?
I am working on creating a website for a swimming club. The idea is to display the count medals won by swimmers at the National and International level. Something like hidden link

I have 3 custom post types Swimmers, Events and Swim Timings. Swimmer and Event are parents of Swim timings with a 1:many relationship.
There is a custom field in Events which mentions the type of event w.g. Local,Regional,National or International It is a select field.

Now in a swimmer page I am unable to query the type of event in the parent post.

I need some guidance on the following

1. Can a custom field be inherited by a child and such that values of the child change if the value of the parent is changed. ( I have existing data)
2. Alternatively if I could make Event type a Taxonomy can it be inherited and is there any specific documentation i could follow?

3. How Can i create a table for the Gold Silver and Bronze and display the count there? Do i need to run a specific view for each of the medals and use [wpv-items-count] and get_view_query_results

My site is in localhost right now. The current website is hidden link which does not have this functionality

Vijay

#1614527

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hello, please find my feedback below.

1. Can a custom field be inherited by a child and such that values of the child change if the value of the parent is changed. ( I have existing data)
2. Alternatively if I could make Event type a Taxonomy can it be inherited and is there any specific documentation i could follow?
Unfortunately inheritance isn't automatic or built-in to Toolset relationships like this, but you could use custom code and the Toolset APIs to achieve either of these options. You could copy the value of custom field from a parent post into all child posts, or you could copy the taxonomy terms associated with a parent post and apply those same terms to all child posts.

To copy the values of a custom field or the terms of a taxonomy, you need to understand at least the following WordPress hooks and Toolset APIs:
save_post
https://developer.wordpress.org/reference/hooks/save_post/
This hook allows you to trigger any PHP code when a post is saved or updated.

toolset_get_related_posts
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
This API allows you to fetch the child posts of a known parent post.

toolset_association_created
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
This API allows you to trigger any PHP code when two posts are connected using a post relationship. For example, when a new child post is connected to an existing parent post, you can trigger code to copy the parent post's custom field into the child post's custom field.

toolset_before_association_delete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_before_association_delete
This API allows you to trigger any code just before two posts are disconnected from one another in a post relationship. You may or may not need to use this API, depending on how you want to handle child field value updates during the disconnection process.

get_post_meta (specifically for copying custom field values)
https://developer.wordpress.org/reference/functions/get_post_meta/
This API allows you to get existing custom field values. These are raw field values from the database with no formatting applied automatically.

update_post_meta (specifically for copying custom field values)
https://developer.wordpress.org/reference/functions/update_post_meta/
This API allows you to update custom field values.

wp_get_object_terms (specifically for copying taxonomy terms)
https://developer.wordpress.org/reference/functions/wp_get_object_terms/
This API allows you to fetch the taxonomy terms associated with a given post.

wp_set_object_terms (specifically for copying taxonomy terms)
https://developer.wordpress.org/reference/functions/wp_set_object_terms/
This API allows you to set the taxonomy terms associated with a given post.

3. How Can i create a table for the Gold Silver and Bronze and display the count there? Do i need to run a specific view for each of the medals and use [wpv-items-count] and get_view_query_results
You can use one View with a custom field Query Filter that accepts a shortcode attribute. This will allow you to pass the medal type into the Query Filter something like this:

[wpv-view name="Your View Name" medal="Gold"]
[wpv-view name="Your View Name" medal="Silver"]
[wpv-view name="Your View Name" medal="Bronze"]

This technique for passing arguments into Views is described in more detail here:
https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/

If you want to use the Views shortcode approach, you could output wpv-found-count in the View to display the number of medals of each type. Place the wpv-found-count shortcode in the View's Loop Output editor inside the wpv-items-found shortcode but outside the wpv-loop tags, otherwise it will be displayed over and over in the results - once per result found.

If you want to use PHP APIs exclusively, you can create your own custom shortcode that displays the number of results of this View using get_view_query_results:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
Example using the arguments array to pass a shortcode attribute like medal="Gold":
https://toolset.com/forums/topic/views-using-php-function-get_view_query_results-ignoring-attributes-passed/
Example creating a custom shortcode and implementing get_view_query_results:
https://toolset.com/forums/topic/get-no-of-results-returned-by-view-in-template-after-all-filters-have-applied/
Please note that this example uses a deprecated post relationship syntax for querying related posts, but should be useful as an example anyway. Let me know if you have questions about these concepts and I can offer more advice.

#1622143

Hi Christian,
Thanks for the explanation. Honestly it was overwhelming.

I would request for your help in a step by step manner

1. I have exported my site to a localhost, I moved the Toolset data using Ultimate CSV import
2. I now need to create two custom fields in my swimtime post type in which data from the parent Event would be copied.
3. How can i update the existing data?
4. For new data i will use the CRED hook cred_save_data

Pls suggest if this format is fine. I shall move on a step by step basis to take this ahead

I could not understand toolset_association_created can you help
Vijay

#1624833

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vijay,

Christian is currently unavailable at the moment so I will be handling this ticket for him.

How can i update the existing data?

Are you referring to data that already have their relationships existing and you want to setup the fields to inherit the values ?

As Christian mentioned, custom coding will definitely be required to accomplish this task.

The toolset_association_created hook essentially is triggered anytime a new relationship is made. This will allow you to do some data manipulation when the relationship association has been made.

So in this hook you can update your custom fields, carry over field values from the parent to the child etc.

Thanks,
Shane

The topic ‘[Closed] Custom field inheritance and count’ is closed to new replies.