Bulk rewriting columns based on custom field value in Wordpress

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

// Set max posts per query
$max = 500;
$total = 5000; 

for($i=0;$i<=$total;$i+=$max) {

$parts = get_posts(array('post_type' => 'parts', 'numberposts' => $max, 'offset' => $i));

    // loop through every part
    foreach ( $parts as $part ) {

    // get part number
    $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 ); // update existing posts
    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) {
     //Check it not an auto save routine
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;

 //Perform permission checks! For example:
    if ( !current_user_can('edit_post', $post_id) ) 
        return; 

    //If calling wp_update_post, unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'my_custom_slug');

    //call wp_update_post update, which calls save_post again. E.g:
if($partno != '')
        wp_update_post(array('ID' => $post_id, 'post_name' =>get_post_meta($post_id,'partno',true)));

    // re-hook this function
    add_action('save_post', 'my_custom_slug');
}
+5
source share
1 answer

1) , site.com/update update.php. update.php :

<?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

} ?>

, , cron- .

:

<?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'); ?>

, (, ) _ .

+2

All Articles