WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i have a custom post type products.How can i add a collection of items to that custom post type like this:

Product A
option n

Option items can be 0-n
In a relational database it would be like this:
table_products (product_id, title,...)
table_options (product_id, option_id,...)

Thank you.

share|improve this question

I think you've got the Custom Post Type a bit wrong, the products are not saved in products table but in the posts table


So to use the options in relation to post_id you can use the build-in custom fields which are key/value pairs.

The key is the name of the meta-data element. The value is the information that will appear in the meta-data list on each individual post that the information is associated with.

either by using the build-in UI or creating a custom metabox by plugin or by code and they are saved in postmeta table


and to access these option you use get_post_meta() function:

$meta_values = get_post_meta($post_id, $key, $single);

for example say you have a field named price so to get its value:

$price = get_post_meta($post_id, 'price', true);

and this will get the price of the product with the same post_id.

hope this helps.

share|improve this answer
i've used both custom post types and custom fields, but i need something different. The "options" aren't key/value pairs. A better example: Custom post type "houses" and custom post type "rooms", how can i associate the rooms with the houses? – Oterox Apr 12 '11 at 17:27
That depends on your needs, you can associate houses with rooms by storring the room post type ID's in a house post type custom field, you can associate by custom taxonomy, you can set room post type as a child post of house post type or you can use post2post plugin that creates a many to many relation between posts, but i think that's a different question and again mostly depends on you needs – Bainternet Apr 12 '11 at 17:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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