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.

I've spent hours trying to understand the custom taxonomies functionality, but still can't figure out how to use it for my situation.

I have Restaurants. They can be a certain type of restaurant, and can be in a certain city. A restaurant can be more than one type but will not be in more than one city. I need to be able to display a list of restaurants by type, and then list them according to the city they're on.

Basically: Chinese Restaurants page, and on that page is a list of cities, under each city is a list of Restaurant names with a link to their page. This particular restaurant can also appear in the Buffet Restaurants page etc.

Any recommendations on the best way to set up a custom post type and custom taxonomies for this? Right now I have the restaurant post type, and restaurant type as the labels..... but after that I'm not sure where to go. How can I add cities to each of these, and then be able to list each one according to the city? This is what I have so far:

add_action('init', 'rest_register');

function rest_register() {

$labels = array(
    'name' => _x('Restaurants', 'post type general name'),
    'singular_name' => _x('Restaurant Type', 'post type singular name'),
    'add_new' => _x('Add New', 'restaurant type'),
    'add_new_item' => __('Add New Restaurant Type'),
    'edit_item' => __('Edit Restaurant Type'),
    'new_item' => __('New Restaurant Type'),
    'view_item' => __('View Restaurant Type'),
    'search_items' => __('Search Restaurants'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rest_icon' => get_stylesheet_directory_uri() . '/rest-admin-icon.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail')
  ); 

register_post_type( 'restaurants' , $args );
}

/*end custom post type for menus*/

register_taxonomy("Restaurant Types", array("restaurants"), array("hierarchical" => true, "label" => "Restaurant Type", "singular_label" => "Restaurant Type", "rewrite" => true));
share|improve this question

1 Answer 1

up vote 0 down vote accepted

It can be daunting to wrap your head around these things sometimes. I sure had a hard time with it :)

You were right making the Restaurant a custom post type which you can then assign terms from your the custom taxonomies.

First of all, it would be best if you change the names of your taxonomies to something more readable. You might be mixing up terms someplace, and then something like restaurant-type or restaurant-location is much clearer.

Then, to assign a city to a post I would use a taxonomy as well. If you use a hierarchical taxonomy (like categories) you could even put a restaurant into a state, county, city and even a part of the city. In an URL this would look really cool, like: /restaurants/california/alameda/fremont/sundale/

Of course a restaurant can't be in two places at once, but clean content is also about the content manager. Depends on if you want to have users categorize their own restaurants. If so, you could do a check when publishing or updating the post so that it can only have one ticked restaurant-location term. This check should ideally be hooked into the save_post action, like so:

function check_for_multiple_tax_terms( $taxonomy ) {

    $tax_to_check = get_terms( $taxonomy );

    if ( count($tax_to_check) > 0 ) {

        // Do stuff when there's more than one term.
        // For example, throw an error.

    }
}
add_action('save_post', 'check_for_multiple_tax_terms');

I hope this helps you out!

share|improve this answer

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.