Support » Plugins » Hacks » Is it possible to automatically populate a custom taxonomy with a value

  • Id it possible to automatically populate a custom taxonomy with a value based on another custom taxonomy’s value?

    How would one go about this?

    Thanks, Pete

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz


    Since you can get terms and add terms by script, yes it’s possible. How you go about it would depend on what event should trigger this process, how you determine what terms to use, what you do with those terms to arrive at the new terms, and what you do with those new terms.

    Without those answers, it’s impossible to say in any more detail than it is possible.

    if custom taxonomy A has a custom term “weejet” then auto populate custom taxonomy B with a custom term of “wadjet”.

    So I guess there’d be somewhere whereby I can edit the code to specify…
    the slug of custom taxonomy A, the custom term value “weejet”, the slug of custom taxonomy A, and the custom term value “wadjet”.


    oh and to stretch the friendship…
    If the user of custom taxonomy A deletes the custom term “weejet” then likewise delete the custom term “wadjet” from custom taxonomy B.

    Moderator bcworkz


    If you were editing the terms somewhere I would not consider this an automatic process. You could of course setup something similar to moderating comments, but that gets somewhat involved.

    For the sake of example, let’s just say you prepend “pete_” to any terms. Predictable alteration of terms becomes even more important if you want automatic deletion as well. You will not be able to identify the matching term to delete if it’s randomly changed, unless you also managed a pairing table that keeps track of matched pairs.

    If you want to duplicate any term in “products” to “pete_products”, you do something like this:

    add_action('created_products', 'pete_copy4me');
    function pete_copy4me( $term_id ) {
       $term = get_term_by( 'id', $term_id, 'products');
       $nu_term_name = 'pete_' . $term->name;
       $nu_term_slug = 'pete_' . $term->slug;
       //consider copying description, parent, etc.
       $args = array('slug'=> $nu_term_slug,); //add description, parent, etc. to this array
       wp_insert_term( $nu_term_name, 'pete_products', $args );

    Removal is similar but different.

    add_action('delete_products', 'pete_delete4me', 10, 3 );
    function pete_delete4me( $term, $tt_id, $deleted_term ) {
       $pete_term_name = 'pete_' . $deleted_term->name;
       $pete_term = get_term_by('name', $pete_term_name, 'pete_products');
       wp_delete_term( $pete_term->term_id, 'pete_products');

    A quality plugin would also include error handling to manage the situation where the database operation failed for some reason. Log the error? Email the admin? Error message to user? Or is it ignored?

    This code is all untested, even if it doesn’t work, it demonstrates the concept correctly.

    I don’t want my terms prepended… does the above code still work then?

    Moderator bcworkz


    That was just an example. The point was you need some predictable way of naming terms in the second taxonomy. That would include no change at all. Without such logic, the relationship needs to be maintained elsewhere if you want to have the auto delete capability.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is it possible to automatically populate a custom taxonomy with a value’ is closed to new replies.