How to retrieve category NAME instead of ID in a function with a taxonomy custom field?

Question

I’m trying to change the post title (both back-end and front-end) for a specific post type through custom field. The custom field I’m using is a taxonomy field where you I choose from different categories (cars). I’m using this code:

    //Save ACF field as post_content for back-end
add_action('save_post', 'change_title_cars');

function change_title_cars($post_id) {
    global $_POST;
    if('cars'== get_post_type())
    {
        $post_custom_title = get_post_meta($post_id,'car_name',true);
        $my_post = array();
                $my_post['ID'] = $post_id;
                $my_post['post_title'] = $post_custom_title;
remove_action('save_post', 'change_title_cars');
                    wp_update_post( $my_post );
add_action('save_post', 'change_title_cars');
    }
   }

//Save ACF field as post_content for front-end
add_action('acf/save_post', 'change_title_frontend_cars');

function change_title_frontend_cars($post_id) {
    global $_POST;
    if('cars'== get_post_type())
    {
        $post_custom_title = get_post_meta($post_id,'car_name',true);
        $my_post = array();
                $my_post['ID'] = $post_id;
                $my_post['post_title'] = $post_custom_title;
remove_action('acf/save_post', 'change_title_frontend_cars');
                    wp_update_post( $my_post );
add_action('acf/save_post', 'change_title_frontend_cars');
    } 
}

And it works fine, except that it shows the category ID (number) in the title instead of the name.

I tried changing Return Value in the custom field setting from Term ID to Term Object, but it didn’t work.

0
, , , , L. core 1 year 2020-07-10T16:10:20-05:00 0 Answers 69 views 0

Leave an answer

Browse
Browse