I need some help with a search plugin: WP Custom Fields Search. I have three drop down menus: 1. City 2. Suburb 3. Cuisine What I need is so that when you select a City, it automatically moves the 2. Suburbs to relevant to that city. 3. is a custom field ie. there can be chinese restaurants in any city. suburb so it doesnt matter but only certain suburbs in certain cities...

So how do I filter it any ideas?


The problem with custom fields is that there is no relation between them so you can't relate Suburbs to cities.

A better way would be to create an hierarchical Custom Taxonomy so you can have a parent - child relation (like categories and sub categories). so:

add_action( 'init', 'create_locations_taxonomies', 0 );

//create two taxonomies, genres and writers for the post type "book"
function create_locations_taxonomies() 
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name' => _x( 'Locations', 'taxonomy general name' ),
    'singular_name' => _x( 'Location', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Locations' ),
    'all_items' => __( 'All Locations' ),
    'parent_item' => __( 'Parent Location' ),
    'parent_item_colon' => __( 'Parent Location:' ),
    'edit_item' => __( 'Edit Location' ), 
    'update_item' => __( 'Update Location' ),
    'add_new_item' => __( 'Add New Locations' ),
    'new_item_name' => __( 'New Location' ),
    'menu_name' => __( 'Locations' ),

  register_taxonomy('location',array('post','page','custom type'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'location' ),

This will add a meta box to the new/edit post screen just like categories but for location so you can add Cities and Suburbs as their children location.

after you have done that you can create a dropdown with the parent locations only (cities) with wp_dropdown_categories() something like this:

$args = array(
    'show_option_none'   => 'select your location',
    'hide_empty'         => 1, 
    'echo'               => 1,
    'selected'           => 0,
    'hierarchical'       => 1, 
    'name'               => 'p-location',
    'class'              => 'postform',
    'depth'              => 1,
    'tab_index'          => 0,
    'taxonomy'           => 'location',
    'hide_if_empty'      => true );
 wp_dropdown_categories( $args );

Then you can use Jquery/ajax to catch the change event of this dropdown and fetch the children (Suburbs) of the selected ID and populate the second dropdown.

Its not complete but it should get you started.

| improve this answer | |

You can also take a look at this great tutorial/document by Kirsty Burgoine with some good explanation on how to do this with custom post types.


| improve this answer | |

Your Answer

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

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