0

I have custom post types product. I want to create related products from same custom post type (product). The user should be able to select posts (products) just as a user selects a category(or more) for the product to be related to)

I think I could create code for that by storing custom values in post_meta table.

Example: In DB I have:

Product1
Product2
Product3
Product4
Product5
Product6
Product7

I edit Product4 and I just want to say that Product1 and Product7 is related to Product4. I could then store product product4_1 with value 1, product4_7 with value 1 in postmeta-table.

I think I'm missing something that really matters here, but can't figure out what. (I see that giving names product4_1 and product4_7 might be problematic).

Not looking for solution, just some guidance to point me in right direction...

  • This question is confusing, can you refer to your 2 post types using different names so it's clearer what you mean? Are you trying to use a custom post type to signify 2 products are related? – Tom J Nowell Sep 15 '14 at 10:10
  • 1
    Sorry for the confusion. I just want somehow to make it possible for logged-in user to set related posts (cpt = product) for a specific post. – bestprogrammerintheworld Sep 15 '14 at 11:33
3

I suspect what you're trying to do is implement related posts, and you're using post meta to indicate which posts are related to the current post.

So if I have a handbag, and there are 5 related products, the handbag product has post meta containing the IDs of those 5 products.

While this sort of works, it doesn't scale, and it isn't performant.

Instead use a custom taxonomy. Do this:

  • hook into product post creation and deletion
  • When a product is saved/delete, create and delete a term in the new taxonomy whose slug is something like "X" where X is the ID of your product. Give the nice pretty name the posts title so users know what they're selecting
  • Now when in a product post, to set related posts, check the posts that're related in your new related products taxonomy
  • On the frontend, to figure out which posts are related, use wp_get_object_terms to get all the terms that product has in the related posts taxonomy.
  • Loop over each term, extract the slug, which is also the ID of the related product, and pass it into get_post() to get a post object
  • Display this post object as you normally would

As a bonus, if your product is related to X, the might be other products related to X, therefore those are probably related too.

The downside of this is you need to manually set for each product what's related. However, you can get around this using a heuristic:

If 2 products are in the same sub category, they're probably related

So to show all related products, just show the products that share the deepest product category. There are questions on how to find the deepest level categories on the site

  • 1
    he is right and I feel, it's best possible solutions – Ram Sharma Sep 15 '14 at 10:41
  • 1
    "I suspect what you're trying to do is implement related posts, and you're using post meta to indicate which posts are related to the current post." YES. I basically should have a custom taxonomy called productrelations or something like that? (and in the taxonomy store product and related product for each row). I just want the loggedin-user to manually set related products (no need for automatic ones) – bestprogrammerintheworld Sep 15 '14 at 11:36
  • Yes! I would personally choose related_products as the taxonomy name but that's at your discretion – Tom J Nowell Sep 15 '14 at 11:46
  • I found a plugin for exactly my needs. It was simply called Related and was exactly what I was looking for. Forgive for not accepting this answer before. But now I will :-) – bestprogrammerintheworld Sep 19 '14 at 8:40
  • Glad you sorted your problem =] – Tom J Nowell Sep 19 '14 at 10:58

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.