Automatic Refresh all posts at once

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.

Read More

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?

Related posts

Leave a Reply

3 comments

  1. 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);
        }
    }
    
  2. 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');