0

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.

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

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' );
}
  • 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? – Ahhhhhhhhhhhhhdfgbv Oct 28 '16 at 11:46
  • Yes. It's the name of a column in the table wp_term_taxonomy. You must not change this. – CodeMascot Oct 28 '16 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.