Custom Taxonomy Relationship (ex: plant classification)

I’m trying to implement plant classification as a custom taxonomy for a custom post type.

For now, plant classification should be on three levels: Family, Genus, Species.

Read More

As I see it, there are two possible ways:

  1. Using taxonomy level as an indicator of taxonomy type, ex:

    plant_class                (custom taxonomy)
      '-- Rhamnaceae           (level 1 = family)
        '-- Ziziphus           (level 2 = genus)
          '-- Ziziphus Jujuba  (level 3 = species)
    

    Advantages:

    • Correct structure; I’d be able to find the family name of a given genus.
    • Administrator just selects a species to classify a plant.

    Disadvantages:

    • Cannot be extended later on (eg; to introduce sub-species)
    • The per-level aspect looks too “magical” to me (unintuitive / confusing to beginners)
  2. Putting each name under the classification type itself, ex:

    plant_class              (custom taxonomy)
     |-- Family               (group of families)
     | '-- Rhamnaceae
     |-- Genus                (group of geni)
     | '-- Ziziphus
     '-- Species              (group of species)
       '-- Ziziphus Jujuba
    

    Advantages:

    • Easily extensible (to add sub-species or even order at a higher level)

    Disadvantages

    • Admin needs to select all applicable classification parts when associating to a plant (eg; a family, genus and species)
    • Programatically, I lose the ability to find family from genus (broken hierarchy)

Another (and even wilder) idea I’ve had is to create several taxonomies (plant_family, plant_genus, plant_species) and somehow form a relation between each taxonomy type so that a family can be associated to several geni. I’ve no idea how this would work.

Wondering why there’s no code? Well, it’s relatively easy to create custom taxonomies, and what I’m missing is purely a system design issue.

Ref: Biological Classification | WordPress Taxonomy Reference | Ziziphus 🙂

Related posts

Leave a Reply

3 comments

  1. 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.
  2. Since I already have a custom taxonomy for plant details, this is what my structure will look like:

     Plant Details (custom taxonomy)
       |- Class Ranks                      classification ranks
       | |- Family               <-.
       | |- Genus                <-|-.
       | '- Species              <-|-|-.
       |- Plant Classes            | | |   plant classification
       | '- Rhamnaceae           --' | |
       |   '- Ziziphus           ----' |
       |     '- Ziziphus Jujuba  ------'
       '- Plant Types                      other plant details...
         |- Cactus
         |- Herb
         '- Tree
    

    This is my proposed answer thanks to input from @s_ha_dum