Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Move/copy custom taxonomy terms on publish_post_type WordPress

  • SOLVED

I'm using a plugin that creates posts with terms in a custom taxonomy.

When the post is published I want to automatically move or copy specific terms in the taxonomy to another taxonomy.

Eg;

Custom post type: food

Custom Taxonomy 1: Fruits
Custom Taxonomy 2: Vegetables

A post is created in 'food' with the following terms in the 'Fruits' taxonomy:

Name=Bananas Slug=bananas, Name=Potatoes Slug=potatoes, Name=Onions Slug=onions, Name=Strawberries Slug=strawberries

On publish_food I want to automatically move the following terms to 'Vegetables':

Name=Potatoes Slug=potatoes, Name=Onions Slug=onions

Can the terms be moved to the second taxonomy with the Name intact or is it easier to copy the terms?

// Auto move terms
add_action('publish_food', 'auto_move_terms');
function auto_move_terms() {

CODE HERE?

}
}

Answers (1)

2015-04-22

Arnav Joy answers:

try this

add_action('publish_food', 'auto_move_terms');

function auto_move_terms($post_id) {

$terms = get_the_terms( $post_id, 'Fruits' );

foreach ( $terms as $term ) {

wp_insert_term( $term->name, 'Vegetables' );
}



}


Austin comments:

That would move all terms from Fruits to Vegetables

I need to move specific terms only, eg; potatoes, onions


Arnav Joy comments:

how do we know you want only specific terms?


Austin comments:

> When the post is published I want to automatically move or copy specific terms in the taxonomy to another taxonomy.


Arnav Joy comments:

there should be a "check condition" or something through which I can know only these terms need to be moved.


Austin comments:

If terms of the new post include onions or potatoes then move only those terms to second taxonomy?


Arnav Joy comments:

now try this

<?php

// Auto move terms

add_action('publish_food', 'auto_move_terms');

function auto_move_terms($post_id) {

$terms = get_the_terms( $post_id, 'Fruits' );

foreach ( $terms as $term ) {
if( $term->slug == 'onions' || $term->slug == 'potatoes' )
wp_insert_term( $term->name, 'Vegetables' );
}




Austin comments:

It copies the terms over to the second taxonomy but there is no post associated with them?


Arnav Joy comments:

yes there will be no posts for them as they are different taxonomies , if you want to assosiate same post to that taxonomy you have to alter register_taxonomy function and have to add that custom post type

https://codex.wordpress.org/Function_Reference/register_taxonomy


Austin comments:

The second taxonomy is registered with the custom post type.


Arnav Joy comments:

you mean with the same cpt ?


Austin comments:

yup


Arnav Joy comments:

try this

<?php

// Auto move terms

add_action('publish_food', 'auto_move_terms');

function auto_move_terms($post_id) {

$terms = get_the_terms( $post_id, 'Fruits' );

foreach ( $terms as $term ) {
if( $term->slug == 'onions' || $term->slug == 'potatoes' ) {
$term_inserted = wp_insert_term( $term->name, 'Vegetables' );
$term_id = $term_inserted['term_id'];
wp_set_post_terms( $post_id, array( $term_id ), 'Vegetables );
}
}



}


Austin comments:

When I create a test post it copies both terms (onions,potatoes) to the second taxonomy but only associates the cpt with the second term (potatoes).


Arnav Joy comments:

try this

<?php

// Auto move terms

add_action('publish_food', 'auto_move_terms');

function auto_move_terms($post_id) {

$terms = get_the_terms( $post_id, 'Fruits' );

foreach ( $terms as $term ) {
if( $term->slug == 'onions' || $term->slug == 'potatoes' ) {
$term_inserted = wp_insert_term( $term->name, 'Vegetables' );
$term_id = $term_inserted['term_id'];
wp_set_object_terms( $post_id, array( $term_id ), 'Vegetables' );
}
}



}


Arnav Joy comments:

did you tried last code , it worked ?


Austin comments:

Just tried it but get the same - both terms are copied but only the second one is associated with the cpt.

When I create a second post I get an error:

Cannot use object of type WP_Error as array in


Arnav Joy comments:

add me on skype : arnav.joy


Austin comments:

Sent skype request


Arnav Joy comments:

try this

$terms = get_the_terms( $post_id, 'fruits' );


foreach ( $terms as $term ) {

if( $term->slug == 'onions' || $term->slug == 'potatoes' ) {

$term_exists = term_exists( $term->name, 'vegetables' );
if ($term_exists !== 0 && $term_exists !== null) {
$term_slugs[] = $term->slug;
}
else{

$term_inserted = wp_insert_term( $term->name, 'vegetables' );
$term_slugs[] = $term->slug;
}

}

}

if( !empty($term_slugs) ){

wp_set_object_terms( $post_id, $term_slugs, 'vegetables' );
}

}