Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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