Updating the posts:
$all_posts = get_posts(
'posts_per_page' => -1,
'post_type' => 'post'
);
foreach ( $all_posts as $single ) {
wp_update_post( array(
'ID' => $single->ID,
'post_title' => to_title_case( $single->post_title ) // see function below
));
}
And, while not pertinent to WP, for the sake of completeness:
function to_title_case( $string ) {
$articles_conjunctions_prepositions = array(
'a','an','the',
'and','but','or','nor',
'if','then','else','when',
'at','by','from','for','in',
'off','on','out','over','to','into','with'
);
$words = explode( ' ', mb_strtolower( $string ) );
foreach ( $words as $position => $word ) {
if (
0 === $position ||
! in_array( $word, $articles_conjunctions_prepositions )
) {
$words[$position] = ucwords( $word );
}
}
$string = implode( ' ', $words );
return $string;
}
This title case conversion function certainly is not perfect - the list of words to be left in lower-case could be expanded, especially by more prepositions. Also it will mess with acronyms and other words that should remain in all-caps.
The WP-specific part is only the upper code block though, anyhow.