Custom Taxonomy Relationship (ex: plant classification)

Why not use a hierarchical taxonomy? I can’t think of a better use for hierarchy than what you are describing. As far as your perceived disadvantages:

Admin needs to select all applicable classification parts when
associating to a plant (eg; a family, a genus and a species)

Rhamnaceae
- Ziziphus
-- Ziziphus Jujuba

If you check Ziziphus Jujuba then that plant post is automatically in the Ziziphus group and also automatically in the Rhamnaceae group.

Programatically, I lose the ability to find family from genus

You can always chase a term “up the tree” by checking a term’s parent. get_term() returns an object with the property parent which is 0 when it is a top-level term and ID number of its parent when it is not.

Say for example you are on a plant post that is classified in the Ziziphus Jujuba group.

// get all the terms for a particular post
$plant_terms = wp_get_post_terms( get_the_ID(), 'plant_class' );

//assuming there are terms to loop through
if( $plant_terms && !is_wp_error($plant_terms)) {

    //lets pop off the first term (shouldn't there be only one?)
    $plant_term = array_shift($plant_terms);

    //check that term's parent ID and follow it upwards
    //when you get to a top-level term $plant_term->parent = 0
    //so the loop will break
    while ( $plant_term->parent > 0 ){
        $plant_term = get_term( $plant_term->parent );
    }

    //this should echo the family name
    echo $plant_term->name;

}

I think you could make that while loop more complex, maybe with a counter, to know when you are on which level.

The potential pitfall that I see is

  1. Limiting the inputs to only the 3rd “species” level. My Radio Buttons for Taxonomies plugin can help you limit to only a singular input, but wouldn’t force it to be the “species” level. (FWIW- it needs an update for a bug fix for adding terms, but I’m working on it). Though maybe you could fork my plugin and use a little jQuery to limit the input.

Leave a Comment

error code: 523