0

In Wordpress we can create a post type which can contain taxonomies (or custom fields). Can these taxonomies (or custom fields) contain anything? For example other taxonomies or something else?

If I need a Wordpress model where I've got people who have got pets who have got toys which have got colours... As you can imagine, two different people can have a cat; a toy for a cat can be at the same time a toy for a dog; the black colour can be a feature of many different toys. All the relationships are "many to many". Is there a simple way of nesting these seperate data types respectively?


Some clarifications if what's above doesn't explain the idea clearly enough.

I'd like to display lists on my Wordpress website.

  • For example a list of people. For each person on the list I'd like to display what's their pet.
  • For example a list of pets (or example a cat as one item). For each pet I'd like to display people who have got this pet. For each pet I'd like to display what toys are available for them.
  • For example a list of colours. For each colour I'd like to display the toys which are available in the colour.

In a programming language I'd create 4 classes and each one would have fields where some of fields are realised by one of the classes (for example a pet would be a field of a class "person"). In Wordpress it seems that the most appropriate thing so far are custom post types. But I don't know if post types can contain another post type as a feature (a characteristic/ a field/ whatever we call it in WP). In the standard WP the post type "post" and the post type "page" are not related like this at all as far as I'm concerned.

3
  • I'm not clear on what you're trying to accomplish - all of the examples you've provided describe flat-level many-to-many relationships. That said a taxonomy can be registered as hierarchical (such as the Category taxonomy) such that it's terms can exhibit parent/child relationships. Custom fields (meta-data) are however uniquely instanced to each post/term/user - meta-data is a one-to-one relationship with it's respective object (though you can use it to represent a custom relationship with other objects)
    – bosco
    May 22 at 16:21
  • 1
    @bosco Thank you for your comment. I tried explaining my idea better in the post. If still some explanations would help you help me, please, contact me back.
    – musialmi
    May 22 at 17:26
  • If I've understood correctly I think you'd just use a taxonomy type of 'colour' (red, blue, black etc) a taxonomy type of 'pet' (dog, cat, badger etc) that toy can belong to any of those taxonomies. IE TOY = blue, cat, & badger. A person can also belong to dog and badger if those are their pets? The query can look for other posts that contain those taxonomy entries
    – Bysander
    May 23 at 11:55

1 Answer 1

0

WordPress content is implemented in two foundational structures: Posts represent distinct "instances" of content, and Taxonomy Terms aggregate and organize Posts according to their shared qualities. Meanwhile "custom fields" - or more specifically Meta-data/"Meta" - can be used to add arbitrary data to both Posts and Terms (among other things).

Analogous to classical OOP, a Post Type would be similar to a sub-class for content, and individual Posts of that type it's instances. Post meta-data operates similarly to class fields/properties in cases where WordPress's parent Post class does not provide the necessary fields.

While there are many ways in which you could implement the proposed project, the more closely you can adhere to WordPress's core ideology, the more you can take advantage of it's built-in functionality such as automatic routing and archive pages/functions to display associated content.

I'd consider implementing the following:

  • Toy is a custom post type
  • Color is a custom (non-hierarchical) taxonomy applied to Toys. This way each Color term aggregates Toys with the same color
  • Person is a custom post type.
  • If pets are just general types of pets rather than specific pets (e.g. "Spike"), I'd make Pet another custom taxonomy, which could apply to both the Person and Toys post-types. This way Toys can can be aggregated based on applicable pets, and Persons can be aggregated based on the pets they own.

The only caveat here is if "people" are actual end-users of the site, in which case you might store the term slugs for owned types of pets in User meta instead of creating another post type (effectively creating a new relationship between a user and a Term which WordPress does not support out-of-the-box). This would mean that you'd need to implement a little more logic for Person-related queries in order to query Toys based on the Pet terms stored in a user's meta, but ultimately it wouldn't add too much complexity.

The above is probably the most direct solution, but you could certainly extend it in many ways, such as creating a Pet post-type as well in order to provide pet information content, also classifying the Pet post-type with the Pet taxonomy in order to facilitate simple query logic.

Finally, if you wanted to start adding hierarchies to the above, you could register the Pets taxonomy to be hierarchical such that say Skink is a child term of Reptile. Or register the Toy CPT as hierarchical in order to represent variations of a Toy or some such.

In short, I think WordPress supports the basic scenario presented fairly-well out of the box. But you can always craft custom relationships between objects. It's all very flexible - you might simply need to put in some work on modifying URL rewrites, queries, and templates in order to represent it all as you intend.

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.