Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

Product A
title
description
price
option1
option2
option3
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

1 Answer 1

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

prefix_posts(post_id,title,content,...

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

prefix_postmeta(meta_id,post_id,meta_key,meta_value)

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

 
discard

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.