Results 1 to 9 of 9

Thread: replace blank titles with the_excerpt

  1. #1
    beachbum's Avatar
    beachbum is offline Hello World
    Join Date
    Aug 2009
    Location
    Miami Beach, FL
    Posts
    5

    Default replace blank titles with the_excerpt

    I'm looking for a plugin or even a sql query(since its like a one time
    thing) that will parse all post titles, and if it is
    "empty/blank/null" it will replace it with the_excerpt(like 5 words
    deep) permenantly in the database, not a client-side replace?

    A plugin would be nice so that it could always be looking so when you
    post with no title it would automatically add the_excerpt(with a
    setting for #chars).

    I am trying to get 20 years of posts that were made before using
    titles for each.

    thanx,
    /s

  2. #2
    itsananderson's Avatar
    itsananderson is offline Big Tipper
    Join Date
    Jan 2009
    Location
    Terre Haute, IN
    Posts
    354

    Default

    I had a chance to create a very rough solution to your problem last night.

    It runs when you enable the plugin (and only then), so any new posts you create without titles won't be fixed (yet). If I have more time tonight I'll try to improve it.

    You'll probably want to edit the $length and $end variables. Right now what happens is if the excerpt is longer than $length, then it's shortened to $length + length($end) and then concatenated with $end.

    As an example, consider if $length is 10 and $end = '...'

    For excerpt "The Quick Brown Fox" it would be shorted to "The Qui..."

    One improvement I might add tonight if I get the chance would be to limit by word length, rather than character. That way you don't cut off any words.

    Anyway, if you want to take a look, I've got a minimal page up for it right now.

  3. #3
    beachbum's Avatar
    beachbum is offline Hello World
    Join Date
    Aug 2009
    Location
    Miami Beach, FL
    Posts
    5

    Default

    tried to take a look at it and got a 404 :(

    A limit by word length would be perfect.

    Just so we're on the same page, Im looking to do this to the database table, not just parse and replace in the loop template client side.

  4. #4
    itsananderson's Avatar
    itsananderson is offline Big Tipper
    Join Date
    Jan 2009
    Location
    Terre Haute, IN
    Posts
    354

    Default

    Sorry, I forgot to change the page's status from "Private". Should be visible now, though I didn't get a chance to work on the plugin last night, so it still only does a character limit. I'll hopefully have some time tonight to look at it again.

  5. #5
    beachbum's Avatar
    beachbum is offline Hello World
    Join Date
    Aug 2009
    Location
    Miami Beach, FL
    Posts
    5

    Default

    that's a great plugin, /me applauds, but it doesn't look like it updates/changes the "post_title database value". It looks like it only does it on a per view thru the theme only. will be looking forward to see with using a word limit insted of characters

  6. #6
    itsananderson's Avatar
    itsananderson is offline Big Tipper
    Join Date
    Jan 2009
    Location
    Terre Haute, IN
    Posts
    354

    Default

    It actually should be changing the post_title value. If you look at your list of posts in the dashboard, it should display their new titles. Also, if you look in the database it should show you the new titles. If that's not happening, please let me know. I might need to fix something.

  7. #7
    beachbum's Avatar
    beachbum is offline Hello World
    Join Date
    Aug 2009
    Location
    Miami Beach, FL
    Posts
    5

    Default

    ok, im gonna wait a bit till you have the "per words" instead of "per characters" done, no rush.

    1 more double check, this only replaces titles that are blank/empty/null correct?

    Thank you so much for all the help. It will be great to have 20 years of posts with the title added.

  8. #8
    itsananderson's Avatar
    itsananderson is offline Big Tipper
    Join Date
    Jan 2009
    Location
    Terre Haute, IN
    Posts
    354

    Default

    Yeah, it just does empty titles.

  9. #9
    beachbum's Avatar
    beachbum is offline Hello World
    Join Date
    Aug 2009
    Location
    Miami Beach, FL
    Posts
    5

    Default Finally figured out a way.

    I finally got my empty post titles replaced and I thought Id share how I did it.

    Its probally the worst code in the world, but it works.

    1. in my themes functins.php I added:
    Code:
    function new_excerpt_length($length) {
    	return 4;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
    function new_excerpt_more($post) {
    	return '';
    }
    add_filter('excerpt_more', 'new_excerpt_more');
    This gave me a new excerpt length of 4 words.

    2. then in index.php or archives.php I added:
    Code:
    				
    if (get_the_title() != '') {$mytitle = get_the_title(); 
    } else {
    $mytitle = get_the_excerpt();
    $badtitles = array(',','!','?','"','\'','<br />','<br>','<div>','</div>'); //yea ima idiot
    $mytitle = str_replace($badtitles, '' , $mytitle);
    $myid = $post->ID; 
    $mytitle = ucfirst($mytitle);
    $wpdb->query("UPDATE wp_posts SET post_title = '$mytitle' WHERE ID = '$myid'" ); 
    
    }
    and replaced the post title display:
    Code:
    the_title();
    with
    Code:
    echo $mytitle;
    3. Then I changed my posts per page reading setting to 50 posts and viewed the each page of the archives normally.

    This totally gave me what I wanted, A four word post title with the first letter capitalized.

    Then I removed the hack code so archive.php was normal again.


    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •