Building relation for two different custom taxonomies

Viewed 13

Hi I've created a custom post type named as stores. Stores includes two custom taxonomies, States :

enter image description here

Cities :

enter image description here

Now I want to show states in cities taxonomy so when user adds a city it should fall under some state.

Reason behind to do this I've created a filter page so whenever user selects state in dropdown it should return cities associated with that state in other dropdown so that my queries can work perfectly Here's how filter are : enter image description here

Right now it's showing cities from all states

Here's the code for registering Custom Post type :

function create_store_function(){
$labels = array(
    'name' => _x('Stores', 'post type general name', 'your_text_domain'),
    'singular_name' => _x('Store', 'post type Singular name', 'your_text_domain'),
    'add_new' => _x('Add Store', '', 'your_text_domain'),
    'add_new_item' => __('Add New Store', 'your_text_domain'),
    'edit_item' => __('Edit Store', 'your_text_domain'),
    'new_item' => __('New Store', 'your_text_domain'),
    'all_items' => __('All Stores', 'your_text_domain'),
    'view_item' => __('View Stores', 'your_text_domain'),
    'search_items' => __('Search Store', 'your_text_domain'),
    'not_found' => __('No Store found', 'your_text_domain'),
    'not_found_in_trash' => __('No Store on trash', 'your_text_domain'),
    'parent_item_colon' => '',
    'menu_name' => __('Stores', 'your_text_domain'),
    
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'store'),
    'capability_type' => 'page',
    'has_archive' => true,
    'hierarchical' => true,
    'menu_position' => null,
    'taxonomies' => array('State'),
    'menu_icon' => 'dashicons-format-gallery',
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail')
);
$states = array(
    'name' => __('State'),
    'singular_name' => __('State'),
    'search_items' => __('Search'),
    'popular_items' => __('More Used'),
    'all_items' => __('All States'),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __('Add new'),
    'update_item' => __('Update'),
    'add_new_item' => __('Add new State'),
    'new_item_name' => __('New'),
    'show_admin_column' => true
);
$cities = array(
    'name' => __('City'),
    'singular_name' => __('City'),
    'search_items' => __('Search'),
    'popular_items' => __('More Used'),
    'all_items' => __('All Cities'),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __('Add new'),
    'update_item' => __('Update'),
    'add_new_item' => __('Add new City'),
    'new_item_name' => __('New'),
    'show_admin_column' => true
);
register_taxonomy('store_state', array('store'), array(
    'labels' => $states,
    'singular_label' => 'store_state',
    'all_items' => 'State',
    'rewrite' => array('slug' => 'state'),
    'hierarchical' => True,
    'public' => true,
    'publicly_queryable' => true,
    'show_in_quick_edit' => true,
    'show_admin_column' => true,
    )
);
register_taxonomy('store_cities', array('store'), array(
    'labels' => $cities,
    'singular_label' => 'store_cities',
    'all_items' => 'Cities',
    'rewrite' => array('slug' => 'city'),
    'hierarchical' => True,
    'public' => true,
    'publicly_queryable' => true,
    'show_in_quick_edit' => true,
    'show_admin_column' => true,)
);
register_post_type('store', $args);
flush_rewrite_rules();

} add_action('init', 'create_store_function');

And searchbar code :

<form action="" method="get" autocomplete="off" id="search-filter">
State 'store_state', 'orderby' => 'name' ) ) ) : echo 'Select category...'; foreach ( $states as $state ) : $count = $state->count; echo 'name . '">' . $state->name . " ($count) " . ''; endforeach; echo ''; endif; ?> City 'store_cities', 'orderby' => 'name' ) ) ) : echo 'Select category...'; foreach ( $cities as $city ) : $count2 = $city->count; echo 'name . '">' . $city->name . " ($count2) " . ''; endforeach; echo ''; endif; ?>

Am I missing something or can it be achieved without creating any relationship between taxonomies and using logics in code?

0 Answers
Related