Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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 );
        unset($my_post);

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

        endif;

    endforeach; 


}

where get_posts and get_post_meta are tweaked to your circumstance.

share|improve this answer
    
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']. –  thelukemcdonald Mar 28 '12 at 23:45
    
going point. alchemy can save to a non-array in extract-mode, but array-mode is default. –  helgatheviking Mar 29 '12 at 13:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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