Usually my posts do not have a title, so in the admin view it always says "(no title)" wherever the title would be shown.

So I am wondering how I could hook into that view function. I would like to check whether the post has no title, and if so, show a custom length excerpt of lets say the first 10 words of the post instead. But only in the admin view, so that in the frontend it only shows a title if there actually is one.

How would I do this?


You can try the following approach using the the_title filter in the edit.php screen:

 *  Modify post titles in the edit.php screen.
 *  If the post title is empty, then show max 10 words from the post content instead.
add_action( 'load-edit.php', function()
    add_filter( 'the_title', function( $title )
        $post = get_post();
        if( is_a( $post, '\WP_Post' ) && ! $post->post_title && $post->post_content )
            $title = wp_trim_words( strip_shortcodes( strip_tags( $post->post_content ) ), 10 );
        return $title;
    } );
} );

where we display max 10 words from the post content if the title is empty.

  • Great stuff! I made this a separate function and also added it to load-index.php, so it also works on the dashboard. Thank you!
    – mcnesium
    May 28 '15 at 13:20
  • you're welcome, great to hear it worked for you @mcnesium
    – birgire
    May 28 '15 at 13:28
  • I just realized, that on the dashboard in the activity box, the titles for the comments are not correctly substituted. It looks like it is using the oldest of the last five contents for all of them, see i.imgur.com/ClQrbDX.png Do you have an idea, why that happens?
    – mcnesium
    Jun 19 '15 at 8:19
  • Brilliant function. This should really be built into WP core for post types formats like aside, link, quote and status
    – Tim Bowen
    Aug 24 '15 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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