• I update a custom fields using pods like the following:

    	
    add_action( 'pods_api_post_save_pod_item_release', 'my_tax_update', 10, 3 );
    function my_tax_update( $pieces, $is_new_item, $id ) {
        
        $terms = $pieces[ 'fields' ][ 'voe_kategorie' ][ '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);
        }
    
    	
      
        wp_set_object_terms( $id, $terms, 'kategorie', false );
      
    }

    This is function perfectly.
    But what if I wanna update a further taxonomy called “genre” from the field “voe_genre” in the same pod “release” ?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator bcworkz

    (@bcworkz)

    Replace all instances of ‘kategorie’ with ‘genre’, and all of ‘voe_kategorie’ with ‘voe_genre’. Unless there’s something odd with Pods data that I’m missing.

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    Replacing makes no sense. I wanna have BOTH genre AND voe_kategorie

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    So like I wrote I wanna update a FURTHER taxonomy keeping this what I wrote so both !

    Moderator bcworkz

    (@bcworkz)

    Ah, I see now, apologies. For only two different taxonomies, you could simply replicate the same code, replacing as I suggested for the second version. But for more than two, the replication starts to be come a little excessive. You could then loop through all of $pieces[ 'fields' ], like so:
    foreach ($pieces[ 'fields' ] as $key => $terms) :
    If the current key is not in an array of keys you’re interested in (‘voe_kategorie’ and ‘voe_genre’, etc.), continue the loop without doing anything for the current key. If the current key is in the array, extract the taxonomy name by stripping off the “voe_” in the key name. Process $terms as usual.

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    Thank you but how I gotte do it for this two exactly I dont get it ??

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    Because if I copy the same code with my new edits behind the first in functions.pho I get an critial php error

    Moderator bcworkz

    (@bcworkz)

    If you copied all of it, the error might be due to a “function already exists” error. Check your error log for specifics, or define WP_DEBUG as true in wp-config.php to see error message on-screen. Also add this just below that line:
    define( 'WP_DISABLE_FATAL_ERROR_HANDLER', true );
    When you’re done debugging, both of these should be set to false on production servers.

    If my guess is correct, either rename the second function, or only duplicate the internal code within the same function.

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    I tryed to duplicate the function renamed. It gives no critical failure but it doesent save the values.

    Moderator bcworkz

    (@bcworkz)

    Did you remember to do another add_action() for the added function?

    If one works but the other doesn’t, it could be the desired data is not in $pieces[ 'fields' ][ 'voe_genre' ][ 'value' ] as expected. Dump out the entire $$pieces array and determine the correct keys for the desired value.

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    Could you please give me a complete example of the code…sorry

    Moderator bcworkz

    (@bcworkz)

    The guts of your original function could be replicated within the same function. I renamed $terms in the replicated part to avoid any chance of carry over from kategorie.

    add_action( 'pods_api_post_save_pod_item_release', 'my_tax_update', 10, 3 );
    function my_tax_update( $pieces, $is_new_item, $id ) {
        
        $terms = $pieces[ 'fields' ][ 'voe_kategorie' ][ '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);
        }
    
        wp_set_object_terms( $id, $terms, 'kategorie', false );
    
        $terms1 = $pieces[ 'fields' ][ 'voe_genre' ][ 'value' ];
        if ( empty( $terms1 ) ) {
            // if there is nothing there set $term to null to avoid errors
            $terms1 = null;
        } else {
            if ( ! is_array($terms1) ) {
                // create an array out of the comma separated values
                $terms1 = explode(',', $terms1);
            }
            
            // ensure all values in the array are integer (otherwise the numerical value is taken as a new taxonomy term instead of an ID)
                $terms1 = array_map('intval', $terms1);
        }	
      
        wp_set_object_terms( $id, $terms1, 'genre', false );
    }

    If katagorie works but genre does not, then the passed $pieces data structure is not as expected.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Update more terms with wp_set_object_terms’ is closed to new replies.