Basically, I have a custom message type setting called "Parts" with over 5000 messages that are currently in it. There are several custom fields associated with each part, including “part number”. Currently URL for each part:
http://site.com/parts/name-of-part/
I would prefer:
http://site.com/parts/XXXX-608-AB/ (This is the part number saved as the custom "partno" field.)
I believe that I need to do two things:
1) Make a script for bulk editing of all bullets for each existing part based on the custom "partno" field.
2) Connect to the Wordpress function to call it, to always create a pool for new parts based on the custom "partno" field.
Does anyone have knowledge on how to accomplish one or both of these aspects?
UPDATE: Below is the code I used to modify existing posts
$max = 500;
$total = 5000;
for($i=0;$i<=$total;$i+=$max) {
$parts = get_posts(array('post_type' => 'parts', 'numberposts' => $max, 'offset' => $i));
foreach ( $parts as $part ) {
$partno = get_post_meta( $part->ID, 'partno', true );
$updated_post = array();
$updated_post['ID'] = $part->ID;
$updated_post['post_name'] = $partno;
wp_update_post( $updated_post );
echo $part->ID;
}
}
UPDATE: The following is the code I used in functions.php to modify current posts . (thanks in part to https://wordpress.stackexchange.com/questions/51363/how-to-avoid-infinite-loop-in-save-post-callback )
add_action('save_post', 'my_custom_slug');
function my_custom_slug($post_id) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if ( !current_user_can('edit_post', $post_id) )
return;
remove_action('save_post', 'my_custom_slug');
if($partno != '')
wp_update_post(array('ID' => $post_id, 'post_name' =>get_post_meta($post_id,'partno',true)));
add_action('save_post', 'my_custom_slug');
}