保存和更新 CPT 的多个条款

问题描述

我无法将多个术语(从复选框数组)更新为自定义帖子类型。我能够阅读和显示当前选定的条款,但无法编辑/更改条款和保存。

我已经尝试了几天来解决这个问题,但没有运气。这个页面似乎是一个类似的问题,但解决方案也不起作用:Saving all values in "multiple select"

简而言之,我有一个自定义分类法,其中包含 5 个显示为复选框的术语:

  1. 类别 1
  2. 类别 2
  3. 类别 3
  4. 类别 4
  5. 第 5 类

如果一个 [CPT] 帖子说的是 term 1 & 3,那么在编辑/更新过程中,我可能想要删除 3 并添加 4,然后更新。

任何见解都会有所帮助。谢谢

function render_rta_main_categories_hook( $form_id,$post_id,$form_settings ) {
    $taxonomy = 'main_categories';  

    // all terms of ctax
    $all_category_terms = get_terms($taxonomy,array('hide_empty' => 0)); 

    // all the terms currently assigned to the post
    $all_post_terms = get_the_terms( $post_id,$taxonomy );

    $name = $taxonomy . '[]';  

    // make an array of the ids of all terms attached to the post
    $array_post_term_ids = array();
    if ($all_post_terms) {
        foreach ($all_post_terms as $post_term) {
            $post_term_id = $post_term->term_id;
            $array_post_term_ids[] = $post_term_id;
        }
    }

    ?>

    <input type="hidden" name="<?PHP echo $name; ?>" value="0" />

    <ul class="wpuf-category-checklist">
        <?PHP  
            foreach($all_category_terms as $term){
                if (in_array($term->term_id,$array_post_term_ids)) {
                    $checked = "checked = 'checked'";
                }
                else {
                    $checked = "";
                }
            $id = $taxonomy.'-'.$term->term_id;
            echo "<li class='wpuf-checkBox-inline'>";
            echo "<label class='selectit'><input type='checkBox' name='$name' id='in-$id'"
            . $checked ."value='$id' /> $term->name<br />";
            echo "</label></li>";
            }
        ?>
    </ul>
    </li>

   <?PHP
}

add_action( 'rta_main_categories_hook','render_rta_main_categories_hook',10,3 );
function update_rta_main_categories_hook( $post_id ) {
    if ( isset( $_POST['main_categories'] ) ) {
        //update_post_Meta( $post_id,'main_categories',$_POST['main_categories']  );
        update_post_Meta( $post_id,'mmain_categories',array_map( 'strip_tags',$_POST['main_categories'] ) );
    }
}
add_action( 'wpuf_add_post_after_insert','update_rta_main_categories_hook' );
add_action( 'wpuf_edit_post_after_update','update_rta_main_categories_hook' );

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)