WordPress.org

Forums

Is it possible to automatically populate a custom taxonomy with a value (7 posts)

  1. Please Add Screenshots
    Member
    Posted 1 year ago #

    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

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. Please Add Screenshots
    Member
    Posted 1 year ago #

    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".

    Cheers

  4. Please Add Screenshots
    Member
    Posted 1 year ago #

    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.

  5. bcworkz
    Member
    Posted 1 year ago #

    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.

  6. Please Add Screenshots
    Member
    Posted 1 year ago #

    I don't want my terms prepended... does the above code still work then?

  7. bcworkz
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic