最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - I Can't get the post_tag name from term_id using wp_set_object_terms

programmeradmin0浏览0评论

I use .html.

I want to display the name of post_data inside widget tags of posts and mainly tags column inside posts list.

I can't get the name or slug of term_id from wp_set_object_terms. update_post_meta serialize automaticaly terms inside metabox. "Foreach" work fine for frontend but not for backend and I have no error.

I tried that and other solutions whith no result:

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_tags'] ) )
    {
        
        update_post_meta( $post_id, 'rudr_select2_tags', $_POST['rudr_select2_tags'] );
        wp_set_object_terms($post_id, $_POST['rudr_select2_tags'], 'post_tag', false) ;
        
    }
    else{
        delete_post_meta( $post_id, 'rudr_select2_tags' );
    }
return $post_id;}}

"wp_set_object_terms($post_id, $_POST['rudr_select2_tags'], 'post_tag')" display only the term_id from serialize code (a:2:{i:0;s:2:"11";i:1;s:2:"23";}. I tried some solutions without results.

I use https://rudrastyh/wordpress/select2-for-metaboxes-with-ajax.html.

I want to display the name of post_data inside widget tags of posts and mainly tags column inside posts list.

I can't get the name or slug of term_id from wp_set_object_terms. update_post_meta serialize automaticaly terms inside metabox. "Foreach" work fine for frontend but not for backend and I have no error.

I tried that and other solutions whith no result:

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_tags'] ) )
    {
        
        update_post_meta( $post_id, 'rudr_select2_tags', $_POST['rudr_select2_tags'] );
        wp_set_object_terms($post_id, $_POST['rudr_select2_tags'], 'post_tag', false) ;
        
    }
    else{
        delete_post_meta( $post_id, 'rudr_select2_tags' );
    }
return $post_id;}}

"wp_set_object_terms($post_id, $_POST['rudr_select2_tags'], 'post_tag')" display only the term_id from serialize code (a:2:{i:0;s:2:"11";i:1;s:2:"23";}. I tried some solutions without results.

Share Improve this question edited Dec 11, 2020 at 20:53 imagIne asked Dec 11, 2020 at 15:40 imagIneimagIne 10512 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Ok. I'm really a beginner. Here is the solution:

$tags = $_POST['rudr_select2_tags'];
            foreach ( $tags as $tag ): 
                            $terms = get_term($tag); 
                            $term = $terms->name;
                            wp_set_post_terms($post_id, $tags_asso, 'post_tag', true) ;
            endforeach;
发布评论

评论列表(0)

  1. 暂无评论