2

I’ve created a custom post type for employees in a company with some fields they have to fill (like E-mail, phone, etc.) and a select box which takes some values from another custom post type called “company”. I’ve disabled / removed the editor for the employee post type but now I’m having trouble listing them in the overview because each “employee” misses a title (post_title). How can I set the post_title for an employee to be equal to the custom field “Name”?

There is a lot of code working so I’ll only show the part where I’m saving my data for each employee:

foreach ($meta_box['fields'] as $field) {
    if(isset($_POST[$field['id']])) {
        if(!add_post_meta($post_id, $field['id'], $_POST[$field['id']], true)) update_post_meta($post_id, $field['id'], $_POST[$field['id']]);
    }

}

I have an array with all the data and then save with the right values for each field but can I here say, that:

If($field[‘id’] == ‘emp_name’) {
    Set($post_title = $field[‘id’]); 
}

Or something like that?

Any help or suggestions would be very much appreciated.

Thank you - Mestika

1 Answer 1

3

I would simply filter the_title so that it outputs the appropriate custom-field data for your custom post type:

<?php
function theme_slug_filter_the_title( $title ) {
    global $post;
    if ( 'employee' == get_post_type( $post ) ) {
        $custom = ( get_post_custom( $post->ID ) ? get_post_custom( $post->ID ) : false );
        $custom_title = ( isset( $custom['name'][0] ) ? $custom['name'][0] : '(Name Not Entered)' );
        return $custom_title;
    } else {
        return $title;
    }
}
add_filter( 'the_title', 'theme_slug_filter_the_title' );
?>

This will replace the post title with the value for custom field "name" for your custom post type. If no "name" is entered, it returns "(Name Not Entered)".

(Of course, you'll need to replace the values for get_post_type() and $custom['name'], as appropriate.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.