1

I'm trying to create a front end taxonomy and term interface.

I currently have:

<form id="updatetax" method="post">
    <input name="term_name" type="text" value="">

    <select name="tax_selection">
    <?php $taxonomies   = get_object_taxonomies( 'cpt_taxonomy', 'objects' ); ?>

        <option value="">Select an option</option>
        <?php
            foreach( $taxonomies as $taxonomy ) {
                echo '<option value="' . $taxonomy->name . '">' . $taxonomy->label . '</option>';
            }

        ?>
    </select>

    <!-- I have a hidden input with the term ID and the current taxonomy if updating -->
    <input name="termID" value="<?php echo $_GET['termID']; ?>">
    <input name="currentTax" value="<?php echo $_GET['currentTax']; ?>">
</form>

Then in the submission, I get the fields and try to update the term:

<?php
    $term_args = array(
        'name'      => $_POST['term_name'],
        'taxonomy'  => $_POST['tax_selection'],
        'term_id'   => $_POST['termID']
    );

    wp_update_term( $_POST['termID'], $_POST['currentTax'], $term_args );

?>

In the codex it seemed that's all was needed to do to update the term. However, though I can get the title to update, I cannot seem to get the taxonomy to change and update.

I was wondering what I am doing wrong. My end goal to it be able to move/update the terms between taxonomies, add new ones from the form above (by selecting the taxonomy in the select field), and add custom term meta.

1
  • Do you want to change the taxonomy or an existing term ? Or create a term for an existing taxonomy ?
    – CodeMascot
    Commented Oct 24, 2016 at 3:04

1 Answer 1

5

wp_update_term() doesn't changes taxonomy. It just updated the existing taxonomy. Say the below code-

$update = wp_update_term( 1, 'category', array(
    'name' => 'Uncategorized Renamed',
    'slug' => 'uncategorized-renamed'
) );

if ( ! is_wp_error( $update ) ) {
    echo 'Success!';
}

This code finds the category which ID is 1, then updates it to the name and slug passed by as a parameter. In the context of my system the category with ID 1 is Uncategorized. So it will rename it.

For changing terms taxonomy there is no default function. Here I've written one for you. Take a look below-

function the_dramatist_change_terms_taxonomy( $term_id, $future_taxonomy ){
    global $wpdb;
    $update = $wpdb->update(
        $wpdb->prefix . 'term_taxonomy',
        [ 'taxonomy' => $future_taxonomy ],
        [ 'term_taxonomy_id' => $term_id ],
        [ '%s' ],
        [ '%d' ]
    );
    return $update;
}

Here $term_id is the term's ID which taxonomy you wanna change and $future_taxonomy is the terms future taxonomy. $future_taxonomy must have to be string like 'category', 'post_tag' or 'any_other_taxonomy'. It actually updates the database value directly. So be careful before you use it. Specially careful if your term has any parent. Cause it is basically updating the taxonomy value at wp_terms_taxonomy table, not any other one. For updating terms taxonomy I've not found any better option.

And for inserting terms to a taxonomy you can use wp_insert_term( $term, $taxonomy, $args = array() ). So you can check that if desired taxonomy is exist or not. If it exists then update it, if it is not then create it. Like below-

$term = term_exists( 'Uncategorized', 'category' );
if ( $term !== 0 && $term !== null ) {
    the_dramatist_change_terms_taxonomy( $term->term_id, $future_taxonomy )
} else {
    wp_insert_term( '...All..The...Parameter..Here' );
}
4
  • I just realised that the function stopped working. I updated the term_taxonomy_id to be just term_id and it is working. Is it a typo?
    – markb
    Commented Oct 28, 2016 at 11:46
  • Yes. It's the name of a column in the table wp_term_taxonomy. You must not change this.
    – CodeMascot
    Commented Oct 28, 2016 at 11:48
  • This works. FYI, though - if, like me, you fire this on the create_term action, so as to immediately move a term when a user adds it via "Add New Tag" in the back-end, the "Add New Tag" page refreshes with an error notice. The move (ie. from "post_tag" to "category" actually completes successfully - but WordPress is unable to refresh the starting page to show the newly-created post_tag, so it throws an error. Commented Aug 23, 2019 at 11:56
  • In your function you'd pass the $term_id, but then that is assigned to the term_taxonomy_id key. Do you mean to pass $term_taxonomy_id to the function and not the $term_id? (My experience is limited, but I've never seen these two tag object keys differ. They're always the same number).
    – user38365
    Commented Aug 12, 2020 at 21:47

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.