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 created a function to save custom fields on publish a post. Something like this.

function create_fields() {  
    global $post;     
    $casa_id = $post->ID;  
    update_post_meta($post->ID, 'casa_id', $casa_id);  
}  
add_action('publish_post', 'create_fields');  

This function saves on a custom field some string.

Now the question:

How can i use this action on older posts? I have 1000 posts and i don't want to refresh all the posts manually, is this possible?

share|improve this question
    
I'm thoroughly confused as to why you want to save the post ID in the post's postmeta... –  Brian Fegter Sep 4 '12 at 18:26

2 Answers 2

you can do something like this:

$args = array(
    'posts_per_page' => 1000,
    'post_type' => 'post'
    );
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) { 
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $casa_id = $post->ID;
        update_post_meta($post->ID, 'casa_id', $casa_id);
    }
}
share|improve this answer
    
Nice idea!It worked :) Thanks –  Sergio Soares Feb 23 '11 at 2:48
    
this didn't work.. Only refresh the last post, don't know why... I created another post to simplify the question. Do u have another solution? Thanks m8 –  Sergio Soares Feb 24 '11 at 0:12
1  
what didn't work? can you explain? try changing posts_per_page to -1 and add 'post_type" => 'any' to args array –  Bainternet Feb 24 '11 at 0:22

It worked. I tryed this:

function actualiza() {  
global $post;  
$args = array(  
    'numberposts' => -1,  
    'post_type' => 'post',  
    );  
$the_query = get_posts( $args );  
if ($the_query) {  
    foreach ($the_query as $post) {  
        $name = $post->post_title;
            update_post_meta($post->ID, 'name_post', $name);
    } 
}
}   
wp_reset_query();  
add_action('wp_head', 'actualiza');
share|improve this answer

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.