0
votes

I created a custom post type with different input fields. For example first name and last name.

So I want this inputs as title in the post column-list. To do this I used the filter option:

add_filter( 'the_title', function( $title ) { 
    $title_firstname = esc_html( get_post_meta( get_the_ID(), 'first_name', true ) ); 
    $last_name = esc_html( get_post_meta( get_the_ID(), 'last_name', true ) );

    $title = $first_name . ' ' . $last_name;
    return  $title; 
} );

Okay this works for me but there is one problem:

All titles form default-posts and pages are gone.

Whta can I do to change only my custom post titles?

Hope somebody can help me :)

Kind reagrds,

Jop

1
What's the custom post called?Andrew Schultz

1 Answers

0
votes

You can put in a conditional check to see what post type is being displayed to modify the title for only those custom posts.

add_filter( 'the_title', 'change_custom_post_title', 10, 2 );

function change_custom_post_title( $title, $id ) {
    global $wp_query;

    if( 'custom_post_type' !== get_post_type( $wp_query->post->ID ) ) 
        return $title;

    return 'My New custom post title';
}