I’ve had a look around for this but nothing has came up (perhaps am not searching good enough?) but I’m trying to set a parent on a taxonomy which is loaded from another taxonomy.

For example I have Car Makes and Car Models, when adding a new model I would like to select its parent (Car Make) not another parent within itself.

Makes cannot have any parents, but can have unlimited children, models cannot have any children and there parent must be a make.

Is this possible? Preferably without a plugin.

/* Makes */
$labels = array(
'name' => _x( 'Makes', 'taxonomy general name' ),
    'singular_name' => _x( 'Make', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Makes' ),
    'all_items' => __( 'All Makes' ),
    'parent_item' => __( 'Parent Make' ),
    'parent_item_colon' => __( 'Parent Make:' ),
    'edit_item' => __( 'Edit Make' ),
    'update_item' => __( 'Update Make' ),
    'add_new_item' => __( 'Add New Make' ),
    'new_item_name' => __( 'New Make' ),
);  
register_taxonomy('makes', 'car', array('hierarchical' => false, 'labels' => $labels, 'query_var' => false, 'rewrite' => false, 'with_front' => false));

/* Models */
$labels = array(
'name' => _x( 'Models', 'taxonomy general name' ),
    'singular_name' => _x( 'Model', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Models' ),
    'all_items' => __( 'All Models' ),
    'parent_item' => __( 'Parent Model' ),
    'parent_item_colon' => __( 'Parent Model:' ),
    'edit_item' => __( 'Edit Model' ),
    'update_item' => __( 'Update Model' ),
    'add_new_item' => __( 'Add New Model' ),
    'new_item_name' => __( 'New Model' )
);
register_taxonomy('models', 'car', array('hierarchical' => true, 'labels' => $labels, 'query_var' => false, 'rewrite' => false, 'with_front' => false));

This question is slightly related: Show WordPress Custom Taxonomy Items Based On a Selected Item From Another Custom Taxonomy however I assume the parent/child elements are from one taxonomy? In my case they need to be two separate taxonomies.

4 Answers
4

I was searching for an answer of the same question, and found a question on SE, that contains the answer: https://stackoverflow.com/a/40868654

In WordPress 3.7, released in 2013, a filter was added that allows manipulating the arguments, used to get the list of terms for the parent dropdown. The filter was also applied on the edit term form at the end of 2014.

So, hooking to the taxonomy_parent_dropdown_args filter allows changing the taxonomy that provides parents. Something like that:

<?php
add_filter( 'taxonomy_parent_dropdown_args', 'alter_parent_taxonomy', 10, 2 );

function alter_parent_taxonomy( $args, $taxonomy ) {

    if ( 'models' != $taxonomy ) return $args; // no change

    $args['taxonomy'] = 'makes';
    $args['depth'] = 1;

    return $args;
}
?>

I haven’t tried this yet, as I am still doing my initial research for the project.

Leave a Reply

Your email address will not be published. Required fields are marked *