0

I have a custom post type called cars. These cars have makes and models. The terms for model are children of the terms for the makes. I can view all of the models in the screenshot below:

List Of Models

Makes are a separate taxonomy to models and so aren't in this screen. So you can set it so that one taxonomy is a parent of another. So I have set the models taxonomy to be a child of make. This then means when I add a new model, I can then set which make is its parent.

This is the code used when registering these taxonomies

public static function addTaxonomy( $slug, $taxonomyName, $post_type, $args = '' ) {

    $pluralName = empty( $args['plural'] ) ? $taxonomyName . 's' : $args['plural'];
    $labels     = array(
        'name'              => _x( $taxonomyName, 'taxonomy general name', 'stm_theme' ),
        'singular_name'     => _x( $taxonomyName, 'taxonomy singular name', 'stm_theme' ),
        'search_items'      => __( 'Search ' . $pluralName, 'stm_theme' ),
        'all_items'         => __( 'All ' . $pluralName, 'stm_theme' ),
        'parent_item'       => __( 'Parent ' . $taxonomyName, 'stm_theme' ),
        'parent_item_colon' => __( 'Parent ' . $taxonomyName . ':', 'stm_theme' ),
        'edit_item'         => __( 'Edit ' . $taxonomyName, 'stm_theme' ),
        'update_item'       => __( 'Update ' . $taxonomyName, 'stm_theme' ),
        'add_new_item'      => __( 'Add New ' . $taxonomyName, 'stm_theme' ),
        'new_item_name'     => __( 'New ' . $taxonomyName . 'Name', 'stm_theme' ),
        'menu_name'         => __( $taxonomyName, 'stm_theme' )
    );

    $defaults = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_in_nav_menus' => false,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => $slug )
    );

    $args                      = wp_parse_args( $defaults, $args );
    self::$Taxonomies[ $slug ] = array( 'post_type' => $post_type, 'args' => $args );

}


public static function register_taxonomies() {

    foreach ( self::$Taxonomies as $taxonomyName => $taxonomy ) {
        register_taxonomy( $taxonomyName, $taxonomy['post_type'], $taxonomy['args'] );
    }

} 

The point of this code is supposed to be that you can add additional taxonomies to the database at any point in the admin area.

What I am now trying to work out is how I can add a drop down list to filter the models based on the make that is their parent? So for example, if they chose Ford as an option, then only the Ford models would be shown. At the moment as you can see in the screenshot, I am only able to view a giant list of all of the models that have been added to the website.

  • Hi - I see no makes there at all. Is this a hierarchical taxonomy with makes as well as models? Or have you made two taxonomies and added a custom parent/child relationship? Seeing your code for registering the taxonomy/-ies (pasted into the question, not linked) might help. – Andy Macaulay-Brook Oct 14 '16 at 10:38
  • Makes are a separate taxonomy to models and so aren't in this screen. So you can set it so that one taxonomy is a parent of another. So I have set the models taxonomy to be a child of make. This then means when I add a new model, I can then set which make is its parent. – DigM Oct 14 '16 at 15:43
  • You can't make a taxonomy the parent of another taxonomy. How have you done that? – Andy Macaulay-Brook Oct 14 '16 at 15:46
  • I have expanded my question to show how the taxonomies are registered. – DigM Oct 14 '16 at 15:48
  • There's no call to register_taxonomy. It still doesn't make one a parent of the other. You can't use variables as the arguments to the localisation functions by the way, it prevents automated discovery of translation strings. – Andy Macaulay-Brook Oct 14 '16 at 15:53

Your Answer

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

Browse other questions tagged or ask your own question.