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 imported over 1000 posts from a Tumblr gallery. All the posts are untitled, but each has a photo and name inside of the post. How do I make take the content of each post and make it the post name?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Hook into the_post – called when the post is actually used – and fill the title. Be aware the slug has to be changed too.

If you are used not to enter a title, hook into save_post too, and let the same code do this for you.

The code

Download on GitHub

<?php
/**
 * Plugin Name: T5 Lazy Title Updates
 * Description: Fill missing post titles from content when a post is called
 * Plugin URI:
 * Version:     2013.01.08.02
 * Author:      Thomas Scholz
 * Author URI:  http://toscho.de
 * Licence:     MIT
 * License URI: http://opensource.org/licenses/MIT
 */

add_action( 'the_post', 't5_lazy_title_update' );
add_action( 'save_post', 't5_lazy_title_update_save', 10, 2 );

/**
 * Add atitle when the post is called in setup_postdata().
 *
 * @wp-hook the_post
 * @param   object $post
 * @return  void
 */
function t5_lazy_title_update( $post )
{
    if ( ! empty ( $post->post_title )
        and strip_shortcodes( $post->post_title ) === $post->post_title
        )
        return;

    $clean_content = wp_strip_all_tags( $post->post_content, TRUE );
    $clean_content = strip_shortcodes( $clean_content );

    if ( '' === $clean_content )
        return;

    $words = preg_split( "/\s+/", $clean_content, 40, PREG_SPLIT_NO_EMPTY );
    $title = implode( ' ', $words );
    $title = rtrim( $title, '.;,*' );
    $slug  = wp_unique_post_slug(
        sanitize_title_with_dashes( $title ),
        $post->ID,
        $post->post_status,
        $post->post_type,
        $post->post_parent
    );

    wp_update_post(
        array (
            'ID'         => $post->ID,
            'post_title' => $title,
            'post_name'  => $slug
        )
    );

    // $post is passed by reference, so we update this property in realtime
    $post->post_title = $title;
    $post->post_name  = $slug;
}

/**
 * Fill title from post content on save
 *
 * @wp-hook save_post
 * @param   int $post_ID
 * @param   object $post
 * @return  void;
 */
function t5_lazy_title_update_save( $post_ID, $post )
{
    t5_lazy_title_update( $post );
}
share|improve this answer
1  
+1 for lazy updating. Iterating over the entire ~1000 item set at once seems like a good way to exceed the max execution time and prematurely terminate the script. –  boscho Jan 8 '13 at 3:26
    
Thanks I used the plugin (above) it worked, well a little too well because a few posts had [gallery] in them and now I have the that word preceding some of the titles...any suggestions, or is that a new question. –  Cecily Jan 8 '13 at 4:08
    
@CecilyWiggins Ah, I did not expect shortcodes, sorry. I made an update, it will fix titles with shortcodes too. –  toscho Jan 8 '13 at 4:16
    
No worries, I should have been more specific. Where can I grab the update? –  Cecily Jan 8 '13 at 4:28
    
@CecilyWiggins It is on GitHub, and the code here is updated too. –  toscho Jan 8 '13 at 4:30

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.