I have a custom meta box, using WPAlchemy, in which I am looking to get rid of. However, I would like to get all posts that have a specific custom meta value set, and insert it into the begininning of the post content, then delete the meta value.

I have a feeling this post gets me close, as I have been able to successfully add content to the beginning of posts, but have been unsuccessful accessing and adding the custom meta value I'm after. I'm also a little unsure of when to hook into WordPress and perform my actions.

Anyone have any suggestions on the best way to go about this? I figured I would build a simple plugin, activate it, let it do it's job, then remove it.

2 Answers 2


what about something like this on activation?

function wpa47153_run_once(){

    $posts = get_posts(array('numberposts' => -1) );

    foreach($posts as $p) :  

        $meta = get_post_meta($p->ID, 'meta_key',true);

        if($meta) :

            $my_post = array();
            $my_post['ID'] = $p->ID;
            $my_post['post_content'] = $meta . "<br/>"  . $p->post_content ;

            // Update the post into the database
            wp_update_post( $my_post );

            //remove the meta key
            delete_post_meta($p->ID, 'meta_key');




where get_posts and get_post_meta are tweaked to your circumstance.

  • Yes, exactly :) When testing, I was trying to do this in the theme itself. When doing it in the plugin, and upon activation, it worked perfectly. Thanks you! I should note for others, with WPAlchemy, meta is stored in an array. So the IF statement and $meta being added used $meta['my_meta_field_key'].
    – Luke
    Mar 28, 2012 at 23:45
  • going point. alchemy can save to a non-array in extract-mode, but array-mode is default. Mar 29, 2012 at 13:18

I would have rather posted this as a comment to helgatheviking's post, but because I have no reputation on this SE site, I'm making a new post. Feel free to delete it if admin's don't find it helpful.

Helga's example works fantastic, but you should also be aware that if you have a custom save handler set up, say something like below that checks for meta boxes in the post screen and saves up update, deletes on empty values, then helga's script will erase all the other meta if you don't temporarily pause the custom save handler. Eg:

add_action('save_post', 'etm\save_custom_fields');
function save_custom_fields( $postId ) {

    //avoid accidental duplication
    return; //remove this when not using helga's function anylonger

    if( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) 

    //add your custom meta fields here
    $fields = array( 'meta-key-1', 'meta-key-2', '...' );

    foreach( $fields as $metaName ):

        //keep a flag if it's been updated
        $new = false;
        //get the existing value
        $old = get_post_meta( $postId, $metaName, true );

        //update the flag with the new value
        if ( isset( $_POST[ $metaName ] ) ) {
            $new = $_POST[ $metaName ];

        //add/update or delete the meta
        if( $new && $new != $old ) {
            update_post_meta( $postId, $metaName, $new, $old ); //also acts to add

        } elseif( !$new )  {
            delete_post_meta( $postId, $metaName, $old );


I made this mistake on a development site just now. Thankfully I backed up the database before running the Helga's script (which works flawlessly on it's own). But, since I'm likely not the only one using custom meta scripts/custom save handling, I thought this to be an important reminder.

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.