Custom Meta Box (SELECT2) Not Saving Taxonomy Terms
-
18-04-2021 - |
Pergunta
/*
* 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' ) );
}
Solução
First of all you saved the the terms value in a post meta table and not following the wordpress conventional method.
To make it connect with default category with that post you need to modify your save_post
action. Check the modified code.
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;
}
Please check wp_set_object_terms() for reference.
Outras dicas
If you want to save your Tune Category as deafult category, you have to use wp_set_post_terms()
with 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;
}
I have tested and it is working fine: https://prnt.sc/qhubbd , https://prnt.sc/qhubjw , https://prnt.sc/qhubf2 , https://prnt.sc/qhubha
Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange