UPDATE_TERM_META()在某些元键上只更新一次

时间:2021-09-17 作者:Michael

我在自定义分类法上设置了4个自定义元字段,出于某种原因,我的2个文本字段的字段值仅在第一次添加到数据库时更新。将其包括在;添加(&Q);或者如果我创建了一个没有它们的分类法,然后将它们包含在;“编辑”;第一次形成。但只要我尝试更改这些值,数据库就不会得到更新。我做错了什么?

这是我的代码:

// Add the fields to the "groups" taxonomy, using our callback function  
add_action( \'groups_add_form_fields\', \'groups_taxonomy_custom_fields\', 10, 2 ); // "Add new" form
add_action( \'groups_edit_form_fields\', \'groups_taxonomy_custom_fields\', 10, 2 ); // "Edit" form
function groups_taxonomy_custom_fields($term) {
    // Get the page
    global $pagenow;

    // Set empty vars
    $organization = \'\';
    $email_domain = \'\';
    $facilitators = array();
    $access_ids = array();

    // If we are on the "add new" page
    if ($pagenow == \'edit-tags.php\') {
        // Table setup
        $cont1 = \'<div class="form-field">\';
        $cont2 = \'\';
        $cont3 = \'</div>\';
        
    // If we are on the "edit" term page
    } elseif ($pagenow == \'term.php\') {
        // Check for existing taxonomy meta for the term you\'re editing
        $term_id = isset($term->term_id) ? $term->term_id : null;
        $organization = get_term_meta( $term_id, \'organization\', true );
        $email_domain = get_term_meta( $term_id, \'email_domain\', true );
        if (get_term_meta( $term_id, \'facilitators\', true )) {
            $facilitators = get_term_meta( $term_id, \'facilitators\', true );
        }
        if (get_term_meta( $term_id, \'access_ids\', true )) {
            $access_ids = get_term_meta( $term_id, \'access_ids\', true );
        }

        // Table setup
        $cont1 = \'<tr class="form-field">  
        <th scope="row" valign="top">\';
        $cont2 = \'</th>  
        <td>\';
        $cont3 = \'</td>  
        </tr>\';
        
    // Else nothing
    } else {
        $cont1 = \'\';
        $cont2 = \'\';
        $cont3 = \'\';
    }

    // Let\'s get all the webinar codes
    $codes = get_terms( array(
        \'taxonomy\' => eri_access_id_taxonomy(),
        \'hide_empty\' => false,
        \'order\' => \'DESC\',
    ) );

    // Now get all the Facilitators
    $users = get_users( array(
        \'role__in\'  => array( \'facilitator\' ),
        \'orderby\'   => \'display_name\',
        \'order\'     => \'ASC\'
    ) );
    ?>  

    <!-- ADD SOME CSS -->
    <style>
    .yoast-notification {
        display: none;
    }
    .editor-training-groups-list {
        background: white;
        max-height: 20em;
        overflow: hidden;
        padding-left: 6px;
        padding-top: 6px;
        width: 94%;
        overflow-y:auto;
    }
    .editor-training-groups-choice {
        margin-bottom: 10px;
        float: left;
        clear: both;
    }
    .editor-code-label {
        vertical-align: top !important;
        display: inline !important;
    }
    .training-type-title {
        font-weight: bold;
        float: left;
        clear: both;
        margin-bottom: 10px;
    }
    </style>

    <!-- ORGANIZATION FIELD -->
    <?php echo $cont1; ?>   
        <label for="organization"><?php _e(\'Organization/Group\'); ?></label>  
    <?php echo $cont2 ?? $cont2; ?>  
        <input type="text" name="organization" id="organization" size="25" value="<?php echo $organization; ?>"><br />  
        <span class="description"><?php _e(\'The full name of the organization or group.\'); ?></span>  
    <?php echo $cont3; ?>

    <!-- EMAIL DOMAIN FIELD -->
    <?php echo $cont1; ?>  
        <label for="email_domain"><?php _e(\'Email Domain\'); ?></label>  
    <?php echo $cont2 ?? $cont2; ?> 
        <input type="text" name="email_domain" id="email_domain" size="25" value="<?php echo $email_domain; ?>"><br />  
        <span class="description"><?php _e(\'Your organization\\\'s email domain name  (after the @ symbol) that users will mostly be using to register. (NOTE: Anybody who registers using an email address with this domain will be automatically added to this training group.)\'); ?></span>  
    <?php echo $cont3; ?>

    <!-- FACILITATORS FIELD -->
    <?php echo $cont1; ?>  
        <span><?php _e(\'Facilitators\'); ?></span>
    <?php echo $cont2 ?? $cont2; ?> 
        <div class="editor-training-groups-list">
            <?php
            // Vars
            $the_users_checked = [];
            $the_users = [];

            // Cycle through each user
            foreach ($users as $user) {
                $is_checked = false;
                $checked = \'\';
                if (in_array($user->ID, $facilitators)) {
                    $is_checked = true;
                    $checked = \' checked="checked"\';
                }

                $id = \'<div class="editor-training-groups-choice">
                    <input type="hidden" value="0" name="facilitators[\'.$user->ID.\']">
                    <input type="checkbox" id="facilitators[\'.$user->ID.\']" name="facilitators[\'.$user->ID.\']" value="1" \'.$checked.\'/><label for="facilitators[\'.$user->ID.\']" class="editor-code-label">\'.$user->display_name.\' <span class="limit-length">(\'.$user->user_email.\'): \'.$user->organization.\'</span></label>
                </div>\';

                if ($is_checked) {
                    $the_users_checked[] = $id;
                } else {
                    $the_users[] = $id;
                }
            }

            echo implode(\'\', $the_users_checked);
            echo implode(\'\', $the_users);
            ?>
        </div>  
        <span class="description"><?php _e(\'Select the Facilitators that will have access to this training\\s dashboard.\'); ?></span>  
    <?php echo $cont3; ?>

    <!-- TRAININGS FIELD -->
    <?php echo $cont1; ?>  
        <span><?php _e(\'Trainings\'); ?></span>
    <?php echo $cont2 ?? $cont2; ?> 
        <div class="editor-training-groups-list">
            <?php
            // Let\'s get all of the codes to create a link
            $tta_names = [];
            foreach ($codes as $code) {
                $code_names[] = strtolower($code->name);
            }

            // Create empty arrays to store them
            $tta_names = [];
            $ita_names = [];
            $uta_names = [];
            $tta_checked = [];
            $ita_checked = [];
            $uta_checked = [];
            $tta = [];
            $ita = [];
            $uta = [];

            // Cycle through each term
            foreach ($codes as $code) {
                $is_checked = false;
                $checked = \'\';
                if (in_array($code->name, $access_ids)) {
                    $is_checked = true;
                    $checked = \' checked="checked"\';
                }
                $code_title = get_the_title( $code->description );
                $li = \'<div class="editor-training-groups-choice">
                    <input type="hidden" value="0" name="access_ids[\'.$code->name.\']">
                    <input type="checkbox" id="access_ids[\'.$code->name.\']" name="access_ids[\'.$code->name.\']" value="1" \'.$checked.\'/><label for="access_ids[\'.$code->name.\']" class="editor-code-label">\'.$code->name.\': <span class="limit-length">\'.$code_title.\'</span></label>
                </div>\';
                
                if (eri_is_TTA($code->name)) {
                    $tta_names[] = strtolower($code->name);
                    if ($is_checked) {
                        $tta_checked[] = $li;
                    } else {
                        $tta[] = $li;
                    }
                } elseif (eri_is_ITA($code->name)) {
                    $ita_names[] = strtolower($code->name);
                    if ($is_checked) {
                        $ita_checked[] = $li;
                    } else {
                        $ita[] = $li;
                    }
                } elseif (eri_is_UTA($code->name)) {
                    $uta_names[] = strtolower($code->name);
                    if ($is_checked) {
                        $uta_checked[] = $li;
                    } else {
                        $uta[] = $li;
                    }
                }
            }

            // Title Extension
            $title_ext = \' Technical Assistance\';
            $top_margin = \'30px\';
            $target = "_blank";

            // Add the title and list items
            if (!empty($tta_checked) || !empty($tta)) {
                echo \'<div class="training-type-title">Targeted\'.$title_ext.\': <a href="/wp-admin/edit.php?post_type=page&codes=\'.implode(\'%2C\',$tta_names).\'" target="\'.$target.\'">(\'.count($tta_names).\')</a></div>\';

                echo implode(\'\', $tta_checked);
                echo implode(\'\', $tta);
            }

            if (!empty($ita_checked) || !empty($ita)) {
                $margin = (!empty($tta_checked) || !empty($tta)) ? \' style="margin-top: \'.$top_margin.\'"\' : \'\';
                echo \'<div class="training-type-title"\'.$margin.\'>Intensive\'.$title_ext.\': <a href="/wp-admin/edit.php?post_type=page&codes=\'.implode(\'%2C\',$ita_names).\'" target="\'.$target.\'">(\'.count($ita_names).\')</a></div>\';

                echo implode(\'\', $ita_checked);
                echo implode(\'\', $ita);
            }

            if (!empty($uta_checked) || !empty($uta)) {
                $margin = (!empty($tta_checked) || !empty($tta)) || (!empty($ita_checked) || !empty($ita)) ? \' style="margin-top: \'.$top_margin.\'"\' : \'\';
                echo \'<div class="training-type-title"\'.$margin.\'>Universal\'.$title_ext.\': <a href="/wp-admin/edit.php?post_type=post&codes=\'.implode(\'%2C\',$uta_names).\'" target="\'.$target.\'">(\'.count($uta_names).\')</a></div>\';

                echo implode(\'\', $uta_checked);
                echo implode(\'\', $uta);
            }
            ?>
        </div>  
        <span class="description"><?php _e(\'Select the Webinar Codes this Training Group has access to.\'); ?></span>  
    <?php echo $cont3; ?>

    <?php
}  

// Save the changes made on the "groups" taxonomy, "add new" form
add_action( \'created_groups\', \'save_groups_new_custom_fields\', 10, 2 );
function save_groups_new_custom_fields( $term_id, $tt_id ) {  
    if ( isset($_POST[\'organization\']) ) {  
        add_term_meta( $term_id, \'organization\', $_POST[\'organization\'], true );
    }
    if ( isset($_POST[\'email_domain\']) ) {  
        add_term_meta( $term_id, \'email_domain\', $_POST[\'email_domain\'], true );
    }
    if ( isset($_POST[\'access_ids\']) ) {
        $aid_checked = [];
        $access_ids = $_POST[\'access_ids\'];
        foreach ($access_ids as $key=>$value) {
            if ($value == \'1\'){
                $aid_checked[] = $key;
            }
        }
        add_term_meta( $term_id, \'access_ids\', $aid_checked );
    }
    if ( isset($_POST[\'facilitators\']) ) {
        $fac_checked = [];
        $facilitators = $_POST[\'facilitators\'];
        foreach ($facilitators as $key=>$value) {
            if ($value == \'1\'){
                $fac_checked[] = $key;
            }
        }
        add_term_meta( $term_id, \'facilitators\', $fac_checked );
    }
}
// Save the changes made on the "groups" taxonomy, "edit" form
add_action( \'edited_groups\', \'save_groups_existing_custom_fields\', 10, 2 ); 
function save_groups_existing_custom_fields( $term_id, $tt_id ) {  
    if ( isset($_POST[\'organization\']) ) {
        update_term_meta( $term_id, \'organization\', $_POST[\'organization\'], true ); // <-- ONLY UPDATING ONCE
    }
    if ( isset($_POST[\'email_domain\']) ) {
        update_term_meta( $term_id, \'email_domain\', $_POST[\'email_domain\'], true ); // <-- ONLY UPDATING ONCE
    }
    if ( isset($_POST[\'access_ids\']) ) {
        $aid_checked = [];
        $access_ids = $_POST[\'access_ids\'];
        foreach ($access_ids as $key=>$value) {
            if ($value == \'1\'){
                $aid_checked[] = $key;
            }
        }
        update_term_meta( $term_id, \'access_ids\', $aid_checked );
    }
    if ( isset($_POST[\'facilitators\']) ) {
        $fac_checked = [];
        $facilitators = $_POST[\'facilitators\'];
        foreach ($facilitators as $key=>$value) {
            if ($value == \'1\'){
                $fac_checked[] = $key;
            }
        }
        update_term_meta( $term_id, \'facilitators\', $fac_checked );
    }
}
有趣的是,我的复选框字段每次都在更新,所以文本字段出现了一些问题。

1 个回复
SO网友:Michael

我想出来了:

update_term_meta( $term_id, \'organization\', $_POST[\'organization\'], true );
应该是

update_term_meta( $term_id, \'organization\', $_POST[\'organization\'] );
其中第四个参数使其唯一。