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.

Is there any way to update all the posts at a time through query or through admin panel. For an example i am having an 1000 posts, how to update all the posts at a time.

Thanks.

share|improve this question
1  
The simple answer is "Yes", you can run any query you want on your DB, there are plenty of hooks and actions available in the WP API that allow you to do updates as well, but your question is a bit too general to provide anything more than "Yes, it's possible." What do you want to update? Do you need it on every post, or just certain posts? Do you need to do it frequently or just one time? What have you tried to do already? –  totels Dec 12 '12 at 13:38
    
I have Migrated my old db to wordpress, i m having thousand posts in my admin panel. If i change the permalink structure to custom all the post show 404 error. so i need to update all the posts. –  Elan Dec 12 '12 at 13:49

1 Answer 1

1) Create a new page and assign a new page template to it, lets say site.com/update and update.php. Inside of update.php write you bulk mechanism:

<?php
// grab all your posts
$parts = get_posts(array('post_type' => 'parts', 'numberposts' => -1,))

// loop through every part
foreach ( $parts as $part ) {
    // get part number
    $partno = get_post_meta( $part->ID, 'parto', true );

    $updated_post = array();
    $updated_post['ID'] = $part->ID;
    $updated_post['post_name'] = $partno;
    wp_update_post( $updated_post ); // update existing posts

}
?>

You could place this anywhere in your theme but I like to create a page for that so I can easily run a cron job with it.

Next the function to change the slug of every newly created post:

<?php
function change_default_slug($id) {
    // get part number
    $partno = get_post_meta( $id, 'parto', true );
    $post_to_update = get_post( $id );

    // prevent empty slug, running at every post_type and infinite loop
    if ( $partno == '' || $post_to_update['post_type'] != 'parts'
      || $post_to_update['post_name'] == $partno )
        return;

    $updated_post = array();
    $updated_post['ID'] = $id;
    $updated_post['post_name'] = $partno;
    wp_update_post( $updated_post ); // update newly created post
}

add_action('save_post', 'change_default_slug');
?>

The code above runs every time a post gets saved (e.g. when published for the first time) and sets a new post_name to the part no.

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.