• Resolved Miriax

    (@miriax)


    Hi all,
    I’m trying to set up two custom content types on a site such that they reference each other. One of the custom types, lets call it Stores, has a title, hours, description, and a list of services offered, for example Grocery, Pharmacy, Seafood.
    I also have a custom type called Services Offered, which is a title, description, and a list of stores that offer each service.
    I’ve set these both up to list all of the available options using a simple MySQL query,

    SELECT post_title, ID
    FROM [+table_prefix+]posts
    WHERE post_type='stores'

    and

    SELECT post_title, ID
    FROM [+table_prefix+]posts
    WHERE post_type='services'

    But now I’m stymied by how to get the two to link such that when editing Store #4 to say that it doesn’t offer Pharmacy services, that it refers to the same data as the Pharmacy Services Offered custom type. The client I’m designing this for wants only to edit this once on either end, not twice.
    Any ideas, suggestions, help? Thanks in advance!

    https://www.ads-software.com/plugins/custom-content-type-manager/

Viewing 1 replies (of 1 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    This is a question all about how to model your data. In a blank-slate ground-up application, you’d establish object-models for “stores” and “services”, and then you’d have a join table to indicate which stores had which services. And that setup would achieve the core tenant of data normalization, namely that data exists in one place so you don’t have to update things in multiple locations.

    This is WP, so the interface and model is simplified, and you don’t actually have a join table to store the mapping. Instead you have a custom field that stores the mapping between stores and services. So you have to decide how you want this to work: either the “stores” or the “services” custom content type needs to include the reference to the other content-type. E.g. creating a store would let you do a multi-select on its available services, or creating a service would let you select available stores. You cannot do it both ways. Just set it up one way or the other and remove one of the relations.

    What that means for your users is that they would go to the ONE content type deemed “master” to edit the join data. E.g. if you set “stores” as the master (i.e. the definition of the Store content type included a relation field pointing to the services content type), then your users would edit the Store record if new services were added to a store. If you set it up the other way around where the services content type included a Relation field mapping to the stores, then the users would go into the relevant service to edit its list of stores. You cannot map it both ways due to the limitations of the architecture. It’s WP… you don’t really have a join table, so you have to choose which content type will hold the info about the relations. Hope that makes sense.

    Also, I don’t recommend using the raw SQL queries unless you’re really comfortable with them. The “Relation” field type (see https://code.google.com/p/wordpress-custom-content-type-manager/wiki/Relation) exist precisely for this type of scenario: they allow one post to reference another, and by extension, that other post’s data.

Viewing 1 replies (of 1 total)
  • The topic ‘Cross-Referencing Custom Content Types’ is closed to new replies.