自定义元框(SELECT2)不保存分类术语

时间:2019-12-31 作者:Mohammad Mehedi Hassan

我有密码,enter image description here

/*
 * Add a metabox
 * I hope you\'re familiar with add_meta_box() function, so, nothing new for you here
 */
add_action( \'admin_menu\', \'rudr_metabox_for_select2_cat\' );
function rudr_metabox_for_select2_cat() {
    add_meta_box( \'rudr_select2\', \'Tune Category\', \'rudr_display_select2_cat_metabox\', \'post\', \'side\', \'high\' );
}
/*
 * Display the fields inside it
 */
function rudr_display_select2_cat_metabox( $post_object ) {
    // I decided to write all the metabox html into a variable and then echo it at the end
    $html = \'\';

    // always array because we have added [] to our <select> name attribute
    $appended_cat = get_post_meta( $post_object->ID, \'rudr_select2_cat\',true ); 
    /*
     * It will be just a multiple select for tags without AJAX search
     * If no tags found - do not display anything
     * hide_empty=0 means to show tags not attached to any posts
     */
    if( $cats = get_terms( \'category\', \'hide_empty=0\' ) ) {
        $html .= \'<p><select id="rudr_select2_cat" name="rudr_select2_cat[]" single="single" style="width:99%;max-width:25em;">\';
        foreach( $cats as $cat ) {
            $selected = ( is_array( $appended_cat ) && in_array( $cat->term_id, $appended_cat ) ) ? \' selected="selected"\' : \'\';
            $html .= \'<option value="\' . $cat->term_id . \'"\' . $selected . \'>\' . $cat->name . \'</option>\';
        }
        $html .= \'<select></p>\';
    }
    echo $html;
}
add_action( \'wp_ajax_mishagetcat\', \'rudr_get_cat_ajax_callback\' ); // wp_ajax_{action}
function rudr_get_cat_ajax_callback(){

    // we will pass post IDs and titles to this array
    $return = array();

    $cat = get_terms( array(\'taxonomy\' => \'category\',\'search\'=> $_GET[\'q\'],\'ignore_sticky_posts\' => 1,));
        foreach ( $cat as $cat ) {  
            // shorten the title a little
            $title = ( mb_strlen( $cat->name ) > 50 ) ? mb_substr( $cat->name, 0, 49 ) . \'...\' : $cat->name;
            $return[] = array( $cat->term_id, $title ); // array( Post ID, Post Title )
        }
    echo json_encode( $return );
    die;
}
//auto_save
add_action( \'save_post\', \'rudr_save_metaboxdata\', 10, 2 );
function rudr_save_metaboxdata( $post_id, $post ) {

    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return $post_id;

    // if post type is different from our selected one, do nothing
    if ( $post->post_type == \'post\' ) {
        if( isset( $_POST[\'rudr_select2_cat\'] ) )
            update_post_meta( $post_id, \'rudr_select2_cat\', $_POST[\'rudr_select2_cat\'] );
        else
            delete_post_meta( $post_id, \'rudr_select2_cat\' );
        if( isset( $_POST[\'rudr_select2_tags\'] ) )
            update_post_meta( $post_id, \'rudr_select2_tags\', $_POST[\'rudr_select2_tags\'] );
        else
            delete_post_meta( $post_id, \'rudr_select2_tags\' );
    }
    return $post_id;
}
//add_script_&_stylesheet
add_action( \'admin_enqueue_scripts\', \'rudr_select2_enqueue\' );
function rudr_select2_enqueue(){

    wp_enqueue_style(\'select2\', \'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css\' );
    wp_enqueue_script(\'select2\', \'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js\', array(\'jquery\') );

    // please create also an empty JS file in your theme directory and include it too
    wp_enqueue_script(\'mycustom\', get_template_directory_uri() . \'/css/mycustom.js\', array( \'jquery\', \'select2\' ) ); 

}
enter image description here但只显示Deafolt类别。

2 个回复
最合适的回答,由SO网友:Pradipta Sarkar 整理而成

首先,您将术语值保存在post元表中,并且不遵循wordpress的常规方法。

要使其与该帖子的默认类别相连接,您需要修改save_post 行动检查修改后的代码。

add_action( \'save_post\', \'rudr_save_metaboxdata\', 10, 2 );
function rudr_save_metaboxdata( $post_id, $post ) {

    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return $post_id;

    // if post type is different from our selected one, do nothing
    if ( $post->post_type == \'post\' ) {
        if( isset( $_POST[\'rudr_select2_cat\'] ) )
        {
            wp_set_object_terms($post_id, $_POST[\'rudr_select2_cat\', \'category\', false) ;
            update_post_meta( $post_id, \'rudr_select2_cat\', $_POST[\'rudr_select2_cat\'] );
        }
        else
            delete_post_meta( $post_id, \'rudr_select2_cat\' );
        if( isset( $_POST[\'rudr_select2_tags\'] ) )
            update_post_meta( $post_id, \'rudr_select2_tags\', $_POST[\'rudr_select2_tags\'] );
        else
            delete_post_meta( $post_id, \'rudr_select2_tags\' );
    }
    return $post_id;
}
请检查wp_set_object_terms() 供参考。

SO网友:Chetan Vaghela

如果要将您的曲调类别保存为Deafolt类别,则必须使用wp_set_post_terms() 具有update_post_meta().

add_action( \'save_post\', \'rudr_save_metaboxdata\', 10, 2 );
function rudr_save_metaboxdata( $post_id, $post ) {

    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return $post_id;

    // if post type is different from our selected one, do nothing
    if ( $post->post_type == \'post\' ) {
        if( isset( $_POST[\'rudr_select2_cat\'] ) )
        {
             update_post_meta( $post_id, \'rudr_select2_cat\', $_POST[\'rudr_select2_cat\'] );
             wp_set_post_terms($post_id, $_POST[\'rudr_select2_cat\'], \'category\', FALSE);
        }
        else
        {
           delete_post_meta( $post_id, \'rudr_select2_cat\' );
           wp_set_post_terms($post_id,\'\', \'category\', FALSE);
        }

        if( isset( $_POST[\'rudr_select2_tags\'] ) )
            update_post_meta( $post_id, \'rudr_select2_tags\', $_POST[\'rudr_select2_tags\'] );
        else
            delete_post_meta( $post_id, \'rudr_select2_tags\' );
    }
    return $post_id;
}
我已经测试过,效果很好:https://prnt.sc/qhubbd , https://prnt.sc/qhubjw , https://prnt.sc/qhubf2 , https://prnt.sc/qhubha