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 have a custom post type with no Title field. These are short status messages, where only the content matters. The RSS feed and permalink display the title as Auto Draft, which is not very useful. Ideally, it would have the post content, or at least the first 10 or so words.

I've tried this function, but it still appears as Auto Draft:

add_filter('title_save_pre', 'save_title');
function save_title($my_post_title) {
        if ($_POST['post_type'] == 'servicestatus') :
          $new_title = wp_trim_words( $_POST['content'], $num_words = 10, $more = null )
          $my_post_title = $new_title;
        endif;
        return $my_post_title;
}

add_filter('name_save_pre', 'save_name');
function save_name($my_post_name) {
        if ($_POST['post_type'] == 'servicestatus') :
          $new_name = wp_trim_words( $_POST['content'], $num_words = 10, $more = null )
          $my_post_name = $new_name;
        endif;
        return $my_post_name;
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

I think save_post action hook is the proper one. Maybe you'll want to insert some checking if the post title is already set ($post_object->post_title), as this code always update the title according to the content.

add_action( 'save_post', 'save_post_wpse_87921', 10, 2 );

function save_post_wpse_87921( $post_id, $post_object ) 
{
    // Auto save?
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )  
        return;

    // Correct post_type
    if ( 'servicestatus' != $post_object->post_type )
        return;

    $new_title = wp_trim_words( $post_object->post_content, $num_words = 10, $more = '' );

    // Unhook this function so it doesn't loop infinitely
    remove_action( 'save_post', 'save_post_wpse_87921' );

    // Call wp_update_post update, which calls save_post again. 
    wp_update_post( array( 
        'ID' => $post_id,
        'post_title' => $new_title
    ));

    add_action( 'save_post', 'save_post_wpse_87921', 10, 2 );
}
share|improve this answer
    
Perfect for my purposes! Thanks very much. –  Magnakai Feb 23 '13 at 10:04

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.