2

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.

2
  • Do you definitely need 2 separate taxonomies? This might be simpler with a single hierarchical taxonomy like make_and_model.
    – sam
    May 19, 2012 at 20:00
  • I tried it that way first, however its for a client not me and theres a 90% chance they will mess it up :p.
    – Elliott
    May 19, 2012 at 20:08

4 Answers 4

3

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.

3
  • Works perfectly, thanks! Oct 16, 2019 at 23:35
  • Still working for 5.4? Also it's unclear if both need to be hierarchical and which dictates the label / naming conventions.
    – MrMesees
    Apr 20, 2020 at 10:46
  • I've tried this and it does kinda work. A problem with this is that terms which have a parent from another taxonomy disappear from the admin UI unless I use the search. This seems to be because WP tries to display children below their parent, and because the parent is from another taxonomy, the parent and consequently its children don't show up in the table.
    – Rapti
    Mar 15, 2023 at 8:05
2

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

3
  • I've never tried using register_taxonomy_for_object_type I always assumed taxonomies were not stored in the post types array and would fail the assertion that it is a post object. May 14, 2012 at 6:17
  • Unfortunately it looks like there is a check in that function to bail early if the provided object type name does not resolve to a post type object. I'm not sure when the check was introduced since the Git blame isn't easy to trace back prior to a large refactor several years ago. Mar 2, 2023 at 21:45
  • And since the Codex has been largely deprecated, the documentation for that function now lives here: developer.wordpress.org/reference/functions/… Mar 2, 2023 at 21:45
1

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.

1
  • Types do the job very well
    – Liko
    Jun 28, 2017 at 0:56
0

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 the edited_${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.

2
  • Thanks for your reply, I've looked into the edit_form_fields hook and that looks the way to go..its a shame it isn't possible to hook onto the "parent" dropdown.
    – Elliott
    May 16, 2012 at 20:50
  • I agree, it's something commonly done with WordPress as a CMS. I wish I had the time to port it into core and submit a patch as an enhancement, but I'm working on getting WordPress to run on a cloud with a json backend opposed to mysql. May 16, 2012 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.