How do I save the title of a post based on a selected field?

0

My problem is as follows, I created a custom post type called "Home Highlights" and inside it the user adds the post they want, these posts come from a select_advanced Metabox , I made a function to pass the selected post to the current post title, however it ends up returning a number I do not know why.

Here's my function and some prints to help with understanding:

add_filter( 'the_title', 'teste', 10, 2 );
function teste( $title, $post_id )
{
    if( $new_title = get_post_meta( get_the_ID(), 'id_do_metabox', true ) )
    {
        return $new_title;
    }
    return $title;
}

    
asked by anonymous 07.06.2016 / 16:46

1 answer

0

The select basically works by showing a pair of key and value . What you are seeing and selecting in the select is just a label, what is being saved in the database is the ID of the post and not the title, so you need to fetch the title from the post ID. Getting the code like this:

add_filter( 'the_title', 'teste', 10, 2 );
function teste( $title, $post_id )
{
    if( $new_title_id = get_post_meta( get_the_ID(), 'id_do_metabox', true ) )
    {
        return get_the_title($new_title_id);
    }
    return $title;
}
    
08.06.2016 / 02:42
___