How to save a post title based on a selected field?

Asked

Viewed 69 times

0

My problem is this, I created a custom post type called "Home Highlights" and within it the user will add the post you want, these posts come from a select_advanced of Metabox, I made a function to pass the post selected to the title of the current post, but it ends up returning a number I do not know why.

Follow my function and some prints to help in 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;
}

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

0

Basically select works by showing a pair of key and value. What you are viewing and selecting in 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 of 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.