get_results( "SELECT term_taxonomy_id, taxonomy, slug FROM " . $wpdb->term_taxonomy . " as tt INNER JOIN " . $wpdb->terms . " as t ON tt.term_id = t.term_id" ); //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter custom_field_mapping $term_values = ' . var_export( $term_values, true ), 0 ); //$debug_data['term_values'] = var_export( $term_values, true ); $all_term_slugs = array(); foreach ( $term_values as $value ) { $all_term_slugs[ $value->term_taxonomy_id ]['taxonomy'] = $value->taxonomy; $all_term_slugs[ $value->term_taxonomy_id ]['slug'] = $value->slug; } //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter custom_field_mapping $all_term_slugs = ' . var_export( $all_term_slugs, true ), 0 ); //$debug_data['all_term_slugs'] = var_export( $all_term_slugs, true ); } /* * 1. Select the term_taxonomy_id(s) from the mediatagger table * for the post_ID (passed to the MLA filter). */ $term_taxonomy_ids = $wpdb->get_col( $wpdb->prepare( "SELECT m.term_taxonomy_id FROM " . $wpdb->prefix . "mediatagger as m WHERE m.object_id = '%d'", $post_ID ) ); //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter get_col $term_taxonomy_ids = ' . var_export( $term_taxonomy_ids, true ), 0 ); $debug_data['term_taxonomy_ids'] = var_export( $term_taxonomy_ids, true ); if ( empty( $term_taxonomy_ids ) ) { //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter empty $term_taxonomy_ids', 0 ); //update_post_meta( $post_ID, 'Media Tagger', var_export( $debug_data, true ) ); return $setting_value; } /* * 2. Select the term_id and taxonomy from the term_taxonomy table * for the term_taxonomy_id. Then, get the "slug" for each term because * the integer term_id won't insert a new term in the destination taxonomy. * * For the "Map all attachments" case, the values are found in the $all_term_slugs array. */ $new_terms = array(); if ( ( NULL == $all_term_slugs ) ) { $term_taxonomy_values = $wpdb->get_results( sprintf( "SELECT tt.term_id, tt.taxonomy FROM " . $wpdb->term_taxonomy . " as tt WHERE tt.term_taxonomy_id IN ( %s )", implode( ',', $term_taxonomy_ids ) ) ); //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter get_results $term_taxonomy_values = ' . var_export( $term_taxonomy_values, true ), 0 ); $debug_data['term_taxonomy_values'] = var_export( $term_taxonomy_values, true ); /* * If the MediaTagger values don't match current WordPress values, just give up. */ if ( empty( $term_taxonomy_values ) ) { //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter empty $term_taxonomy_values', 0 ); //update_post_meta( $post_ID, 'Media Tagger', var_export( $debug_data, true ) ); return $setting_value; } $term_ids = array(); foreach ( $term_taxonomy_values as $value ) { $term_ids[ $value->term_id ] = $value->term_id; } //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter $term_ids = ' . var_export( $term_ids, true ), 0 ); $debug_data['term_ids'] = var_export( $term_ids, true ); $term_values = $wpdb->get_results( sprintf( "SELECT t.term_id, t.slug FROM " . $wpdb->terms . " as t WHERE t.term_id IN ( %s )", implode( ',', $term_ids ) ) ); //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter get_results $term_values = ' . var_export( $term_values, true ), 0 ); $debug_data['term_values'] = var_export( $term_values, true ); $term_slugs = array(); foreach ( $term_values as $value ) { $term_slugs[ $value->term_id ] = $value->slug; } foreach ( $term_taxonomy_values as $value ) { if ( isset( $term_slugs[ $value->term_id ] ) ) { $new_terms[ $value->taxonomy ][] = $term_slugs[ $value->term_id ]; } } } else { foreach ( $term_taxonomy_ids as $value ) { if ( isset( $all_term_slugs[ $value ]['slug'] ) ) { $new_terms[ $all_term_slugs[ $value ]['taxonomy'] ][] = $all_term_slugs[ $value ]['slug']; } } } //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter $new_terms = ' . var_export( $new_terms, true ), 0 ); $debug_data['new_terms'] = var_export( $new_terms, true ); /* * 3. Use wp_set_object_terms() to assign the taxonomy and term_id value(s) * to the post_ID. You can either replace any existing terms or append the new terms. * * This code handles the case where MediaTagger assignments come from multiple taxonomies, * i.e., "Tags and Categories". It also lets you change the destination taxonomy for each source. * Uncomment the line(s) in $taxonomy_change and enter the destination taxonomy to re-assign * the destination taxonomy. */ $taxonomy_change = array ( // 'post_tag' => 'attachment_tag', // 'category' => 'attachment_category' ); $term_arrays = array (); foreach ( $new_terms as $key => $value ) { if ( isset( $taxonomy_change[ $key ] ) ) { $key = $taxonomy_change[ $key ]; } $term_arrays[ $key ] = $value; } //error_log( "MLAMediaTaggerExample::mla_mapping_rule_filter {$post_ID} term_arrays = " . var_export( $term_arrays, true ), 0 ); $debug_data['term_arrays'] = var_export( $term_arrays, true ); foreach( $term_arrays as $key => $value ) { /* * Set the last argument to true to append, not replace, existing terms */ wp_set_object_terms( $post_ID, $value, $key, false ); } /* * 4. Optionally, remove the object_id and term assignments from the * MediaTagger table. Set the if test to true to include this step. */ if ( false ) { $delete_result = $wpdb->delete( $wpdb->prefix . 'mediatagger', array( 'object_id' => $post_ID ), array( '%d' ) ); //error_log( 'MLAMediaTaggerExample::mla_mapping_rule_filter $delete_result = ' . var_export( $delete_result, true ), 0 ); $debug_data['delete_result'] = var_export( $delete_result, true ); } /* * Uncomment this line to write debugging information into the 'Media Tagger' custom field */ //update_post_meta( $post_ID, 'Media Tagger', var_export( $debug_data, true ) ); /* * $setting_value is an array containing a mapping rule; see above * To stop this rule's evaluation and mapping, return NULL */ return $setting_value; } // mla_mapping_rule_filter } //MLAMediaTaggerExample /* * Install the filter at an early opportunity */ add_action('init', 'MLAMediaTaggerExample::initialize'); ?>