Update multiple taxonomies from values stored in relationship fields
-
I have a few relationship fields related to taxonomies, and I would like to update all the taxonomies from the values stored in each relationship field.
Following the example provided in this article I was able to update one taxonomy. But this code seems to only work for the first taxonomy and not the others. Here’s a sample of the code I have in functions.php:
This first part is working:
add_action( 'pods_api_post_save_pod_item', 'anio_update', 10, 3 ); function anio_update( $pieces, $is_new_item, $id ) { // get the values of the 'anios_rel' field $terms = $pieces[ 'fields' ][ 'anios_rel' ][ 'value' ]; if ( empty( $terms ) ) { //if there is nothing there set $term to null to avoid errors $terms = null; } else { if ( ! is_array($terms) ) { //create an array out of the comma separated values $terms = explode(',', $terms); } //ensure all values in the array are integer (otherwise the numerical value is taken as a new taxonomy term instead of an ID) $terms = array_map('intval', $terms); } //Set the term for taxonomy 'anio' with $terms wp_set_object_terms( $id, $terms, 'anio', false ); }
This part comes right after the first one, and is not updating the taxonomy:
add_action( 'pods_api_post_save_pod_item', 'pais_update', 10, 3 ); function pais_update( $pieces, $is_new_item, $id ) { // get the values of the 'pais_rel' field $terms = $pieces[ 'fields' ][ 'pais_rel' ][ 'value' ]; if ( empty( $terms ) ) { //if there is nothing there set $term to null to avoid errors $terms = null; } else { if ( ! is_array($terms) ) { //create an array out of the comma separated values $terms = explode(',', $terms); } //ensure all values in the array are integer (otherwise the numerical value is taken as a new taxonomy term instead of an ID) $terms = array_map('intval', $terms); } //Set the term for taxonomy 'pais' with $terms wp_set_object_terms( $id, $terms, 'pais', false ); }
With this code only the first taxonomy gets updated, regardless of what taxonomy is first. I’ve tested every part of the code by itself, and it works. The problem is when there is more than one taxonomy to be updated.
Thanks for your help!
- The topic ‘Update multiple taxonomies from values stored in relationship fields’ is closed to new replies.