0

I have a working method for adding Term Meta for Taxonomy Terms. CPT is 'houses' and Taxonomy is 'house_feature'. This is my code...

First, possible values in an array:

$feature_groups = array(
    'bedroom'   =>  __( 'Bedroom', 'genesis' ),
    'living'    =>  __( 'Living Room', 'genesis' ),
    'kitchen'   =>  __( 'Kitchen', 'genesis' )
);

Then, extend the WordPress Term Edit Form, using the {$taxonomy}_add_form_fields Hook dinamically:

add_action( 'house_feature_add_form_fields', 'add_feature_group_field', 10, 2 );
function add_feature_group_field( $taxonomy ) {

    global $feature_groups;

    ?>
    <div class="form-field term-group">
        <label for="feature-group"><?php _e( 'Feature Group', 'genesis' ); ?></label>
        <select class="postform" id="equipment-group" name="feature-group">
            <option value="-1"><?php _e( 'none', 'genesis' ) ?></option>
        <?php foreach ( $feature_groups as $_group_key => $_group ) : ?>

            <option value="<?php echo $_group_key; ?>" class=""><?php echo $_group; ?></option>

        <?php endforeach; ?>
        </select>
    </div>
    <?php

}

Then, save the Term Meta using the created_{$taxonomy} Hook, so it is 'created_house_feature':

add_action( 'created_house_feature', 'save_feature_meta', 10, 2 );
function save_feature_meta( $term_id, $tt_id ) {

    if ( !empty( $_POST['feature-group'] ) ) {

        $group = sanitize_title( $_POST['feature-group'] );
        add_term_meta( $term_id, 'feature-group', $group, true );

    }

}

The update routine using {$taxonomy}_edit_form_fields Hook to get the Field for the group into the Edit Form:

add_action( 'house_feature_edit_form_fields', 'edit_feature_group_field', 10, 2 );
function edit_feature_group_field( $term, $taxonomy ) {

    global $feature_groups;

    // Get current group.
    $feature_group = get_term_meta( $term->term_id, 'feature-group', true );

    ?>
    <tr class="form-field term-group-wrap">
        <th scope="row"><label for="feature-group"><?php _e( 'Feature Group', 'genesis' ); ?></label></th>
        <td>
            <select class="postform" id="feature-group" name="feature-group">
                <option value="-1"><?php _e( 'none', 'genesis' ); ?></option>
                <?php foreach ( $feature_groups as $_group_key => $_group ) : ?>

                    <option value="<?php $_group_key; ?>" <?php selected( $feature_group, $_group_key ); ?>><?php echo $_group; ?></option>

                <?php endforeach; ?>

            </select>
        </td>
    </tr>
    <?php

}

Save the edited data hooking into edited_{$taxonomy}:

add_action( 'edited_house_feature', 'update_feature_meta', 10, 2 );
function update_feature_meta( $term_id, $tt_id ) {

    if ( !empty( $_POST['feature-group'] ) ) {

        $group = sanitize_title( $_POST['feature-group'] );

        update_term_meta( $term_id, 'feature-group', $group );

    }

}

Display Term Meta data in a new column in the Term list table:

add_filter( 'manage_edit-house_feature_columns', 'add_feature_group_column' );
function add_feature_group_column( $columns ) {

    $columns['feature_group'] = __( 'Group', 'genesis' );

    return $columns;

}

Adding the content into the Column Fields, we can use the Hook pattern manage_{$taxonomy}_custom_column:

add_filter( 'manage_house_feature_custom_column', 'add_feature_group_column_content', 10, 3 );
function add_feature_group_column_content( $content, $column_name, $term_id ) {

    global $feature_groups;

    if ( $column_name !== 'feature_group' ) {
        return $content;
    }

    $term_id        = absint( $term_id );
    $feature_group  = get_term_meta( $term_id, 'feature-group', true );

    if ( ! empty( $feature_group ) ) {
        $content .= esc_attr( $feature_groups[ $feature_group ] );
    }

    return $content;

}

Make the group column sortable:

add_filter( 'manage_edit-house_feature_sortable_columns', 'add_feature_group_column_sortable' );
function add_feature_group_column_sortable( $sortable ) {

    $sortable['feature_group'] = 'feature_group';

    return $sortable;

}

Right, all this comes from a tutorial which works just fine. This code creates, edits, saves and deletes only one value that you may select. I would like to be able to select more than one value in a multiselect. My question is: how can I make a <select multiple...> and be able to save, update, etc every multiple values selected? Also, can they be shown in the columns?

Thanks in advance.

1
  • You can shorten your if checks if you use !empty( instead of isset and '' != – Tom J Nowell Sep 27 '17 at 13:05
0

I found the solution, all credit goes to @gmazzap, who gave this great answer in this post: How do I save each option in a multiple select menu as it's own meta_key + meta_value pair?

add_action( 'created_house_feature', 'save_house_feature_meta', 10, 2 );
function save_house_feature_meta( $term_id, $tt_id ) {

    $old_feature_group = get_term_meta( $term->term_id, 'feature-group, true' );
    $new_feature_groups = isset( $_POST['feature-group'] ) ? $_POST['feature-group'] : array();

    if ( empty( $new_feature_group ) ) {

        // No Feature Groups selected. Then, completely delete all Meta values for the Term.
        delete_term_meta( $term_id, 'feature-group' );

    } else {

        $already_feature_group = array();

        if ( ! empty( $old_feature_group ) ) {

            foreach ( $old_feature_group as $value_feature_group ) {

                if ( ! in_array( $value_feature_group, $new_feature_group ) ) {

                    // This value was selected, but now it isn't, so delete it.
                    delete_term_meta( $term_id, 'feature-group', $value_feature_group );

                } else {

                    // This value is already saved, we can skip it from saving.
                    $already_feature_group[] = $value_feature_group;

                }

            }

        }

        // We don't save what is already saved.
        $to_save_feature_group = array_diff( $new_feature_group, $already_feature_group );

        if ( ! empty( $to_save_feature_group ) ) {

            foreach ( $to_save_feature_group as $feature_group ) {

                add_term_meta( $term_id, 'feature-group', $feature_group, false );

            }

        }

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.