Skip Navigation

[Résolu] Best relational structure for Ingredients and Nutrients in a Recipe Site

This support ticket is created Il y a 4 années et 9 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 7 réponses, has 2 voix.

Last updated by Kenneth Horn Il y a 4 années et 9 mois.

Assisted by: Beda.

Auteur
Publications
#1285069

I am beginning work on a website. Recipes primarily. The backbone of the recipe page looks like this: hidden link

I need to develop the database queries to accomplish my goals. We only need to look at the two sections for Ingredients and Nutrition for this inquiry.

Ingredients:
That section has three fields: Quantity, Units and Item (description) fields. The Quantity is a variable-data custom types field, units and item are both a fixed list (possibly select drop-downs?).
There will be up to 10 ingredients per recipe.

Nutrition:
Fixed-data items which at a minimum will contain the name of the Nutrient and a percentage of the daily recommended amount for that nutrient.
The nutrients are what you see on the example page.

The variable field for Nutrients in the Nutrition Section (the percentage field) will be based on a non-variable amount for each nutrient based on 100 g weight of that ingredient (or for vitamins and minerals a smaller unit but that doesn’t affect our structure).
e.g. The RDA for Carbohydrates (based on a 2,000 calorie diet) is 275 g of carbs per day

So, our database needs to have the name and the RDA in Grams for that nutrient, for up to 30 nutrients.

For Ingredients we need to have the Item Name (e.g. Spinach) and the Nutrient value for 100 grams of that Ingredient. For example: 100 g of Spinach has 3.6 g of Carbohydrates

As I stated, there will be up to 10 different ingredients in a Recipe (currently, for SEO reasons related to the permalink, I’ve made WP Posts (post type) my Recipe post type. There are about 35 Nutrients.

An example: A Creamed Spinach recipe might have:

100 g Spinach
3.3 oz Cream (I’ll have to convert fluid oz to grams but I can likely figure out how to do that)

I will want to pull the data from the fields for the Nutrients (custom-post or taxonomy?), compute the total % of RDA for each nutrient for each Ingredient in the recipe, then TOTAL the percentages of each nutrient for each Ingredient for the Recipe. That TOTAL will then be displayed in that NUTRIENTS section on the page.

I will, of course, have to register a calculate plugin to accomplish the mathematical computations but at this juncture I just want to know the best way to structure the site for these objectives.

I am wanting your opinion as to whether the best way is to make Nutrients and Ingredients custom post types related to Recipes and to each other OR perhaps using taxonomies and repeating fields (which I think may limit my ability to display the data in the correct format)?

P.S. I know nothing about Repeatable Groups. I have not had the occasion to use them yet. Since the Ingredients is a static list I would prefer not to create multiple custom fields with that list of items.

#1285441

I think our Documentation for relational posts is actually based on recipes:
https://toolset.com/documentation/post-relationships/#benefits-of-using-related-posts
This might help you to build your custom recipe site as well.

However, Toolset does not calculate, so computing totals is not something you can solve with Toolset (at least, not without Custom Code based on pure PHP Functions).
We have a few examples on how to calculate on the front end with Views displayed data, but that will likely not help for every aspect of this custom logic:
hidden link

As to whether Nutrients and Ingredients should be custom post types related to Recipes and to each other OR perhaps using taxonomies and repeating fields, I would say you create post Types and set them in relation to each other.

But you need to know this:
1. Toolset Post Relationships, even if Many To Many, cannot connect the SAME post to the SAME post again. You can have each relationship only ONCE.
2. That means, if you ever will have twice a pinch of salt in ONE recipe you will not be able to add TWICE the salt post to the recipe. If you ever need to have the same post TWICE or more in the relation, you need to apply something called the "Appearance Post". This basically is the "appearance" of salt in the recipe. It may be once, twice, trice.
So each time Salt appears in the same recipe, you would also be creating a new Appearance post so to connect salt and recipe again.
But this is a dumb example, it is better described using the "Tool". You might be using the Spoon more than once in the same recipe and might want to add Tools to your recipes. In this case, again you would need the post type "Appearance of Spoon in Recipe" to add the spoon more than once to the same recipe!
I hope I could explain this "logic" limitation well enough.
It is not something happening often but needs to be considered.
4. When you say the Ingredients have Quantity, Units and Item (description) fields, I assume these are quantities related to the recipe, not hardcoded. I mean, a recipe may receive 2 pinches of salt (quantity) and another, 4. So you need these Fields not in the ingredient post type, but in the post type that connects the Ingredient to the recipe.
Hence you will create Many To Many relationships between recipe and ingredients because those can have Custom Fields in the intermediary post, which is necessary to add the specific quantity of ingredient depending on the recipe.
5. Nutrition instead could be a Custom Field in the Ingredient Post type, because no matter how much salt, the nutrition is measured by units of XY grams or whatever weight you use. Hence, the fields to store these values can be added to the Ingredient type.

Later, you will have to get the Intermediary Post (with our relationships API most likely) to multiply the Ingredient "nutrition" value by the value of Quantity stored in the related Intermediary Post, but that is another topic - which I could help limitedly but in another ticket, I suggest, once we have completed this setup

I would suggest, to start with a testing site, eventually follow https://toolset.com/documentation/post-relationships/#benefits-of-using-related-posts, and then from there, we would make the logic to compute stuff where needed.

If everything is achievable to your desire, later you can then just pick the Modules with the Module Manager from your testing site and deploying them on the live site 🙂

#1285719

Beda: I'm proceeding in the manner you described above. My implementation of displaying the Ingredient and the variable fields inside the Recipe (post) is a bit awkward. You might have an idea about accomplishing it more eloquently. More on that later.
I'd like to tackle the Nutrients hurdle now. Typical scenario: Five (5) Ingredients (a cpt) in a Recipe (a WP Post). Each Ingredient has 30 types fields that are numbers (values) indicating the amount of a particular nutrient in 100 g of that ingredient. Once the 5 Ingredients are related to the Recipe I need to extract and SUM the values for the five posts in the query (for each of the 30 nutrients). I will then display the total.
I believe I can manipulate my quantities of ingredients (type fields in the Recipes) and the nutrient values using the Calculate function in PHP in the manner described in this Ticket. https://toolset.com/forums/topic/math-with-custom-fields-values/
Either using the functions in this PHP calculate plugin or something native in Views shortcodes, or some simple PHP code, is there a way to simply aggregate the values of those fields in the five Ingredients post in the loop related to a the recipe?

#1285723

I did some more searching and found this ticket that seems on point. I'm not PHP programmer but let me look at it and see if I can figure out how to apply it to my scenario. https://toolset.com/forums/topic/sum-of-fields-in-a-view-2/
Thanks

#1285725

I would just alter the variable data in blue, right? What is 'number-posts' in that code snip? The rest is pretty straight-forward and easily understood, even by a novice like me 🙂

#1285791

I used the short plugin for summing the code and it works beautifully for summing that field in ALL the posts of that post type. Is there an easy modification to limit the summed results to those posts that are children of the post where the shortcode results are viewed? Thanks so much.

#1288421

1. Once the 5 Ingredients are related to the Recipe I need to extract and SUM the values for the five posts in the query (for each of the 30 nutrients). I will then display the total.

You can calculate with PHP, but Toolset has no inbuilt methods for that.
You'd need to create your own custom logic to use in Views for example, there is a starting sample here for this:
hidden link

2. The code at https://toolset.com/forums/topic/sum-of-fields-in-a-view-2/ counts all values stored in fields of all posts in a post type. So yes, you could use it, to sum up, all values of a certain field in several posts.
You would, however, need a different Query to get the posts, as you would be getting related posts as in a Toolset Relationship, hence you'd need toolset_get_related_posts to get those posts and then their fields
We could discuss this in a new ticket if you'd need help with this. The API is explained here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Since this ticket is about the structure, we should split any follow-up question or issue in new tickets.

Thank you!

#1292779

The display of required fields, including displaying values converted from Imperial to Metric was accomplished with two post types, one for the Recipe which contained fields for Quantity, Units, a Conversion factor for displaying Metric versions when Imperial measurements were in the Units field, and Notes for each of up to 10 ingredients in a Recipe. And a related (child in a many to many relationship) post called Ingredients.
My only hurdle now is summing the values of fields in a view that queries the Ingredients. I will open a new ticket for this.

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