1
votes

I have four different taxonomy terms defined in my wordpress theme which are

  • Continents
  • Countries
  • States
  • Cities

My problem is that I am not able to figure out how can I make it possible for user to specify parent-child relationship in terms of different taxonomy. for example, if user adds 'Asia' under continents, 'India' under country, 'Delhi' under state and 'New Delhi' under city, how to specify India as child of Asia, Delhi as child of India and New Delhi as child of Delhi Please help me. thanks.

code for custom taxonomy is this: `

add_action('init', 'register_my_taxes_states');

function register_my_taxes_states() {


 register_taxonomy( 'states',

array (
          0 => 'schools',
          1 => 'universities',
          2 => 'institutes',
          3 => 'colleges',
        ),
    array( 'hierarchical' => true,
        'label' => 'states',
        'show_ui' => true,
        'query_var' => true,
        'show_admin_column' => false,
        'labels' => array (
      'search_items' => 'state',
      'popular_items' => '',
      'all_items' => '',
      'parent_item' => '',
      'parent_item_colon' => '',
      'edit_item' => '',
      'update_item' => '',
      'add_new_item' => '',
      'new_item_name' => '',
      'separate_items_with_commas' => '',
      'add_or_remove_items' => '',
      'choose_from_most_used' => '',
    )
) ); 
}

`

1

1 Answers

1
votes

I've never created a relationship like this using separate taxonomies. The easiest solution I can think of using taxonomies would be to create only a single taxonomy, and then use the built in parent/child functionality.

I personally don't like using taxonomies in most cases. I find that I run into issues that your facing with relationships, and my clients don't seem to find it very intuitive. I prefer to do this through the use of custom post types, rather than taxonomies. Based on what you were describing above, here's what I would do.

  1. Create 4 custom post types (Continent, Country, State, City) http://codex.wordpress.org/Post_Types
  2. Download / Install the plugin "Posts to Posts" which will create a relationship between different post types https://wordpress.org/plugins/posts-to-posts/
  3. Register a connection type between "Continent & Country", "Country & State", "State & City". https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage

You may then want to create a relationship between "City & School", based on your code above. This way you can go in and select the City that a School is in, and you will also have the data for the State, Country & Continent.

Hope this helps.