Question

I'm working on building a recipe/meal planning site in WordPress. I need to have recipes indexable and sortable by the ingredients required, and I'm trying to figure out the best way of doing that.

Ideally, I would like to have ingredients in one taxonomy, and utensils/appliances required in another taxonomy. I'm coming up against a stumbling block, though, because I can't think of any simple way to associate measurements with the ingredients.

Where I'm at now is using custom fields for the ingredients and values for the measurements and units, like this:

Meta Key: eggs Meta Value: 3|eggs

Meta Key: flour Meta Value: 2.75|cups

and so on.

My question is, is there a better way of structuring this information?

Related, there apparently is a microformat specification for recipes, which is essentially the format I plan on using for display. There is a WordPress plugin (hRecipe) to generate this output format, but its not semantic enough for my liking. If/when I figure out a better way of storing this kind of data, I'll probably release it as plugin...

Was it helpful?

Solution

It's a good idea to use a custom taxonomy to link your ingredients, as this will allow you to get recipes by ingredient. This also means you don't have to store each individual ingredient as a separate meta key-value pair, as you won't do any lookups there. Just store one big object, ingredients, as an ordered list of ingredients and their quantities and units. Read it out when you display the page.

So something like this:

$ingredients = array(
    'eggs' => array(4),
    'whole wheat flour' => array(1, 'cup'),
    'baking powder' => array(1.5, 'teaspoon'),
);

You could also use the ingredient slug or id as the index of the array, then you can cross link from the ingredient list.

By separating the number and the units, you can adjust the numbers for number of guests. Say your default is 4 and I have 6 guests, you just multiply everything by 1.5. You can even include a US to metric conversion! But I'll stop before this becomes a Seasoned Advice question!

OTHER TIPS

My suggestion would be to only store the ingredient names as a custom taxonomy and store everything else about how the ingredients map to the recipe in meta. So you would end up linking a recipe to both the eggs and flour ingredient terms but also saving post meta that would be a serialized array of '{ingredient term id}' => object['quantity'=>{value}, 'unit'=>{unit type}].

This should allow you to easily rebuild the original ingredient list while also an easy way to pull in recipes by an ingredient.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top