cf_custom_fields_save_terms

Save taxonomy terms.

Description

cf_custom_fields_save_terms( (array) $tax_fields, (int) $post_id ); 

Parameters (2)

0. $tax_fields (array)
Taxonomy fields to save
1. $post_id (int)
The post id.

Usage

  1. if ( !function_exists( 'cf_custom_fields_save_terms' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'caldera-custom-fields/includes/to-post-type.php'; 
  3.  
  4. // Taxonomy fields to save 
  5. $tax_fields = array(); 
  6.  
  7. // The post id. 
  8. $post_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = cf_custom_fields_save_terms($tax_fields, $post_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/to-post-type.php  
  1. function cf_custom_fields_save_terms( $tax_fields, $post_id ) { 
  2. if ( is_array( $tax_fields ) ) { 
  3. foreach ( $tax_fields as $taxonomy => $data ) { 
  4. if( empty( $data[ 'terms' ] ) ) { 
  5. continue; 
  6. $terms = $data[ 'terms' ]; 
  7. if( is_numeric( $terms ) && false === strpos( $terms, ', ' ) ) { 
  8. $terms = (int) $terms; 
  9.  
  10. }elseif( is_string( $terms ) && false != strpos( $terms, ', ' ) ) { 
  11. $terms = explode( ', ', $terms ); 
  12. foreach( $terms as $i => $term ) { 
  13. $terms[ $i ] = intval( $terms[ $i ] ); 
  14. }elseif ( is_string( $terms ) ) { 
  15. $terms = get_term_by( 'slug', $data[ 'terms' ], $data[ 'taxonomy'] ); 
  16. if( is_a( $terms, 'WP_Term') ) { 
  17. $terms = $terms->term_id; 
  18. }else{ 
  19. $terms = get_term_by( 'name', $data[ 'terms' ], $data[ 'taxonomy'] ); 
  20. if( is_a( $terms, 'WP_Term') ) { 
  21. $terms = $terms->term_id; 
  22. }else{ 
  23. continue; 
  24. } elseif( is_array( $terms ) ) { 
  25. //yolo(?) 
  26. }else { 
  27. continue; 
  28.  
  29. $updated = wp_set_object_terms( $post_id, $terms, $data[ 'taxonomy'] ); 
  30.  
  31.  
  32.  
  33.  
  34. return true;