Taxonomies for custom post types

Hy guys,

I have some problems with my taxonomies… here’s my code:

Read More
<?php 
define('REVIEWS_SLUG', 'review');
define('REVIEWS_CATEGORY_SLUG', 'review-category');

function create_reviews_section(){                                    
  $labels = array(
    'name' => __('Reviews'),
    'singular_name' => __('review'),
    'add_new' => __('Add New'),
    'add_new_item' => __('Add New Review'),
    'edit' => __('Edit'),
    'edit_item' => __('Edit Review'),
    'new_item' => __('New Review'),
    'view' => __('View'),
    'view_item' => __('View Review'),
    'search_items' => __('Search Reviews'),
    'not_found' => __('No reviews found'),
    'not_found_in_trash' => __('No reviews found in Trash'),
  );

  register_post_type('reviews', array(
        'labels' => $labels,
        'description' => __('Reviews Section'),
        'public' => true,
        'show_ui' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'supports' => array('title', 'editor', 'custom-fields', 'excerpt', 'comments', 'author'),
        'rewrite' => array('slug' => REVIEWS_SLUG, 'with_front' => true),
        'query_var' => false,
    'taxonomies' => array('review-category')        
      )
  );    
}

function create_review_category(){ 
  $labels = array(
    'name' => _x('Review Categories', 'Review Categories'),
    'singular_name' => _x('Review Category', 'Review Category'),
    'search_items' =>  __('Search Review Categories'),
    'popular_items' => __('Popular Review Categories'),
    'all_items' => __('All Review Categories'),
    'edit_item' => __('Edit Review Category'), 
    'update_item' => __('Update Review Category'),
    'add_new_item' => __('Add New Review Category'),
    'new_item_name' => __('New Review Category'),
    'separate_items_with_commas' => __('Separate review categories with commas'),
    'add_or_remove_items' => __('Add or remove review categories'),
    'choose_from_most_used' => __('Choose from the most used review categories'),
    'menu_name' => __('Categories')
  ); 

  register_taxonomy('review-category', array('reviews'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array('slug' => REVIEWS_CATEGORY_SLUG),
  ));
}

First problem is that WordPress doesn’t recognize this taxonomy as hierarchical. For example when I generate a dropdown with this taxonomy and depth="1", it doesn’t return all taxonomies (parents & children).

Second problem, when I’m trying to insert a new term (with wp_insert_term) WordPress is telling me that the taxonomy is not valid.

Third problem, I thought that child taxonomy url will be like: example.com/review-category/parent-taxonomy/child-taxonomy/ but in my case is: example.com/review-category/child-taxonomy/. How I can modify the url to be something like that?

Thanks in advance 😉

LE:

<?php
add_action('init', 'create_reviews_section');
add_action('init', 'create_review_category', 0);

$args = array(
  'show_option_none' => 'Choose make',
  'hide_empty' => 0,
  'name' => 'vehicle_category',
  'id' => 'vehicle_category',
  'taxonomy' => 'review-category',
  'hierarchical' => 0,
  'class' => 'maxw',
  'depth' => 1
); 
wp_dropdown_categories( $args );

Related posts

Leave a Reply

1 comment

  1. For question one, reading the following from the codex makes me think you need to change hierarchical to 1:

    depth (integer) (optional) The max depth. This is ignore unless
    hierarchical is set to true. Default: 0/False

    hierarchical (boolean) (optional) Display all categories (0/False) or
    display categories (1/True) to a depth of ‘depth’. Default: 0/False