Sometimes when developing something on WordPress its necessary to use field term_group from the data base table wp_term_relationships. For example sorting taxonomies terms by this field. But how to assign term_group value to each term when there is no such interface by default? Described below PHP class will help you to resolve this task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | class TaxonomyTermGroup { public function init($taxonomies) { if (!empty($taxonomies)) { foreach ($taxonomies as $taxonomy_name) { add_filter('manage_edit-' . $taxonomy_name . '_columns', array(__CLASS__, 'draw_taxonomy_columns')); add_filter('manage_' . $taxonomy_name . '_custom_column', array(__CLASS__, 'draw_taxonomy_columns_data'), 5, 3); add_action($taxonomy_name . "_edit_form_fields", array(__CLASS__, "edit_form_fields"), 10, 2); } } } public static function draw_taxonomy_columns($columns) { return array_merge($columns, array('term_group' => __('Term group', ''))); } public static function draw_taxonomy_columns_data($value = '', $column_name = '', $term_id = 0) { switch ($column_name) { case 'term_group': $term = get_term_by('term_id', $term_id, @$_GET['taxonomy']); if (is_object($term)) { echo $term->term_group; } else { echo 0; } break; } } public static function edit_form_fields($term, $taxonomy) { ?> <tr class="form-field form-required"> <th scope="row" valign="top"><label for="term_group"><?php _e('Term group', '') ?></label></th> <td> <select name="term_group" id="term_group"> <?php for ($i = 0; $i < 8; $i++): ?> <option value="<?php echo $i ?>" <?php echo($i == $term->term_group ? "selected" : "") ?>><?php echo $i ?></option> <?php endfor; ?> </select> </td> </tr> <?php } } |
Using of this class is simple. After code of initialization taxonomies you should add next piece of code:
1 2 | $taxt=new TaxonomyTermGroup(); $taxt->init(array('clients','skills')); |
clients and skills here is slugs of taxonomies.
