What is the best way to create a hierarchy structure (also called parent/child relationships) between custom post types?

For example, we currently have 3 post types: States, Cities, and Restaurants. Each Restaurant will be in a city, and each city will of course be in a state. So for example, Joe's Crab Shack would be in the city of Orlando, which would be in the state of Florida. I understand taxonomy for the most part, but I'm not sure how to actually "set" the relationship between ex: Joe's Crab Shack/Orlando, and Orlando/Florida. Is this done through taxonomy as well, a custom field type, etc? We also plan on importing these through an Excel document.

Structure Example:

  • URLs: www.site.com/florida/orlando/joes-crab-shack/
  • Breadcrumbs: state->city->restaurant

Pages (only need to know how to retrieve the results):

  • State pages should be able to list all of their cities.
  • City pages should be able to list all of their restaurants.
  • Why not the one post type and two taxonomies? – Butuzov Nov 2 '13 at 21:03
  • I'm not quite sure what you mean, can you elaborate a little more? As of now, states/cities/restaurants all are their own custom post type, as they all have separate sets of custom fields & page templates. – Daniel Nov 2 '13 at 21:12
  • I think you are going about this incorrectly. I think what would be better is to create one custom post type "Restaurants". Then, you create two taxonomies; "Cities" and "States" which belong to the "Restaurants" CPT. Now, you can assign each restaurant CPT to a city and a state. Then, querying your results for your pages is much easier. – josh Nov 3 '13 at 2:31
  • 1
    I am confused as to how this would work. Both Cities & States I see as custom post types, since they both have custom fields (around 10-15), a custom template, and will be imported from an Excel file (around 4k rows for cities). Are all these possible with taxonomy as well? Would seem strange having 4k taxonomy terms in my book, though I am new to this. – Daniel Nov 4 '13 at 14:52
  • 1
    @Codex73 - Forgive my memory, but I believe we ended up just keeping States/Cities/Restaurants as custom fields without any actual 'hierarchy/relationship'. Hopefully that helps a little. – Daniel Feb 8 '18 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.