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.
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:
I haven’t tried this yet, as I am still doing my initial research for the project.
I am not sure, but look at
register_taxonomy_for_object_type
– it connect even taxonomies.If it will not help, your only one solution is combination of custom-taxonomy and custom post type
I know you said no plugins, but can I make a suggestion to use a highly configurable plugin which handles custom fields / groups, custom post types, and custom taxonomies?
Really great, simple, and easy to use plugin. No hardcore advertising. Doesn’t bog down your wordpress website. This plugin is also developed and updated actively.
Download Types – Custom Fields and Custom Post Types Management
I use this plugin on almost every wordpress install. Should be fantastic for what you’re trying to do.
This is something I do rather frequently. WordPress isn’t designed for it out of the box currently.
Unfortunately I don’t know of a way to do it without a plugin (done properly anyhow, using options is an ugly alternative). I have a standard taxonomy class in a plugin that does the heavy lifting for me. It adds custom tax meta tables to the database and provides an abstract custom taxonomy class.
The taxonomy class handles a metadata registry and automates the creation of meta boxes. It has a few built in meta parameters as well such as parent item. I only use the hierarchical attribute for simple taxonomies (parent is same tax) and built-ins.
Update:
Well, for one I was thinking of my post types with different post object parents, but similar approach.
Looking through some of my past works with taxonomies, what I have done before I created my framework plugin with the taxonomy class is echo a dropdown of the parent terms when editing the taxonomy using the
${taxonomyname}_edit_form_fields
hook. You need to provide a callback to theedited_${taxonomyname}
hook as well to save your parent data somewhere. You can mark hierarchical to false for both and store the parent id where wordpress would normally store it.