Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

im developing a logic for populating 3 custom taxonomies into 3 drop downs, i have a custom post type dealers and under that i have 3 taxonomies 1. State 2. City 3. Manufacturer

when i select the state i have to populate the cities that are related to the state, and when i select the city and i have to populate the manufacturers list in the 3rd drop down that is related to the city that i have selected... This is the example link that i am working on

http://carandhalf.com/dealers

here is the code for displaying the custom tax in drop down

<li id="categories">
    <h2><?php _e('Locate Dealer'); ?></h2>
    <form action="<?php bloginfo('url'); ?>" method="get">
        <div>
            <?php $state_args = array('taxonomy' => 'state', 'hide_empty' => 0, 'depth' => 2); ?>
            <?php wp_dropdown_categories($state_args); ?>
        </div></br>
        <div>
            <?php $city_args = array('taxonomy' => 'city', 'hide_empty' => 0); ?>
            <?php wp_dropdown_categories($city_args); ?>
        </div></br>
        <div>
            <?php $man_args = array('taxonomy' => 'manufacturer', 'hide_empty' => 0); ?> 
            <?php wp_dropdown_categories($man_args); ?>
        </div></br>
            <input type="submit" name="submit" value="view" />
        </div>
    </form>
</li>

can anyone help me for filtering the data...... im not a pro in WP, plz bare with me.

thanks in advance

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Without having some kind of relationship defined between the taxonomies its very hard to achieve and not scalable, meaning that you would have to:

  • catch the change of the state (first) dropdown and then get a list of all posts with that taxonomy term (first Query)
  • then loop over all posts and get the list of cities these posts are in (second query) avoiding duplicates and return that list

you would have to repeat that for the change of the cities (second) dropdown.

Now the problem here is with the second query which is actually lots and lots of small queries and there number will only grow. So that is what i mean not scalable.

A better solution would be to use one taxonomy with hierarchical structure where the top level terms are states, the second level terms are cities and the 3rd level are manufacturers, when you are set up in this way the flow becomes:

  • catch the change of the state (first) dropdown and then get a list of terms with that taxonomy term as parent (first Query) and return that list

that's it but this also has a major con and that is the managing of the terms becomes a pain in the $%^ since you would have the same manufactures over and over in different cities.

Another Solution would be to use 3 different taxonomies but also create a custom database table for holding relationship between taxonomy terms.

And the last solution i can think of is using 3 different taxonomies and creating a custom SQL query to get the needed terms (which requires a major ninja like SQL skills that i sadly do not own :) )

Once you figure out which road you are taking then it's a simple matter of creating an ajax call on a change event and populating the next dropdown with the results.

share|improve this answer
    
hi Bainternet, thanks for the response.... –  Solomon Henry Mar 27 '12 at 11:54
    
hi Bainternet, thanks for the response...what if i create 2 taxonomies and 2 drop downs 1. state and 2. manufacturer, so state has a child that is cities. so when i filter the state and city based on the result can i get the manufacturers list in the drop down, is it possible Bainternet ? –  Solomon Henry Mar 27 '12 at 12:05
    
@SolomonHenry anything is possible but as i say without some kind of relation between taxonomies it's not smart and hard to do. –  Bainternet Mar 27 '12 at 12:08
    
@Bainternet.... if i create a 3 custom tables for the 3 taxonomies, i have to inject the data manually to the tables, but the client will be adding the new data from the dashboard then how can i update these tables the minute when the client hits the publish button... –  Solomon Henry Mar 27 '12 at 12:14
    
You only need on custom table if you want to use 3 taxonomies which will hold all relations, as for the data it can be inserted programmatic when a term is created using the provided hook. –  Bainternet Mar 27 '12 at 12:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.