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...

  • One other thing. In order to make logical sense, the ingredients listing has to be in a consistent order, something thats not guaranteed by either custom fields or taxonomies. So... I'm adding yet another level of metadata to be stored. Nov 4 '10 at 22:49

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!

  • Roughly the same answer as below, but double +1 for thinking ahead to conversions and adjustments. Thanks! Nov 5 '10 at 16:47

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.

  • Thanks. I had hoped to avoid duplicating data (having a custom field linked to a taxonomy), but this might just be the best way. I started trying to do everything as custom fields, but there's no easy way to do 'tax__not_in' queries against custom fields. Nov 5 '10 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.