Convert many posts from having a specific meta_key to use a post_format

After much reading, i realized that it would be more easily done using the WordPress API, so i baked out this little script. Simply add it to your theme’s functions.php then run it once. When done, remove it.

function convertGalleries($test){
    $galleries = get_posts(array( 'meta_key' => 'custom_post_template', 'post_status' => array( 'any' ),'posts_per_page'=>-1 ));
    if($test){
        return count($galleries);
    }
    $result = array();
    foreach($galleries as $g){
        $result[$g->ID]= set_post_format($g->ID, 'gallery' );
    }
    return $result;
}

$converted = convertGalleries(false);
echo 'result: '.count($converted). ' posts converted: <pre>';
print_r($converted);
exit;

tech