cf_custom_fields_save_terms
Save taxonomy terms.
Description
Parameters (2)
- 0. $tax_fields (array)
- Taxonomy fields to save
- 1. $post_id (int)
- The post id.
Usage
if ( !function_exists( 'cf_custom_fields_save_terms' ) ) { require_once ABSPATH . PLUGINDIR . 'caldera-custom-fields/includes/to-post-type.php'; } // Taxonomy fields to save $tax_fields = array(); // The post id. $post_id = -1; // NOTICE! Understand what this does before running. $result = cf_custom_fields_save_terms($tax_fields, $post_id);
Defined (1)
The function is defined in the following location(s).
- /includes/to-post-type.php
- function cf_custom_fields_save_terms( $tax_fields, $post_id ) {
- if ( is_array( $tax_fields ) ) {
- foreach ( $tax_fields as $taxonomy => $data ) {
- if( empty( $data[ 'terms' ] ) ) {
- continue;
- }
- $terms = $data[ 'terms' ];
- if( is_numeric( $terms ) && false === strpos( $terms, ', ' ) ) {
- $terms = (int) $terms;
- }elseif( is_string( $terms ) && false != strpos( $terms, ', ' ) ) {
- $terms = explode( ', ', $terms );
- foreach( $terms as $i => $term ) {
- $terms[ $i ] = intval( $terms[ $i ] );
- }
- }elseif ( is_string( $terms ) ) {
- $terms = get_term_by( 'slug', $data[ 'terms' ], $data[ 'taxonomy'] );
- if( is_a( $terms, 'WP_Term') ) {
- $terms = $terms->term_id;
- }else{
- $terms = get_term_by( 'name', $data[ 'terms' ], $data[ 'taxonomy'] );
- if( is_a( $terms, 'WP_Term') ) {
- $terms = $terms->term_id;
- }else{
- continue;
- }
- }
- } elseif( is_array( $terms ) ) {
- //yolo(?)
- }else {
- continue;
- }
- $updated = wp_set_object_terms( $post_id, $terms, $data[ 'taxonomy'] );
- }
- }
- return true;
- }