Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk rewrite post slugs based on custom field value in Wordpress

Basically I have a custom post type setup called "Parts" with over 5,000 posts currently in it. There are a number of custom fields associated with each part, including a "part number". Currently, the URL for each part is:

http://site.com/parts/name-of-part/

What I would rather have is:

http://site.com/parts/XXXX-608-AB/ (That's a part number, stored as a custom field "partno".)

I believe I need to do two things:

1) Make a script to bulk edit all the slugs for each existing part, based on the custom field "partno".

2) Hook into a Wordpress function to trigger it to always create the slug for new parts based on the custom field "partno".

Does anyone have any knowledge on how to accomplish one or both of these aspects?

UPDATE: Below is the code I ended up using for changing 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: Below is the code I used in functions.php to change ongoing 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's 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');
}
like image 732
Trevor Gehman Avatar asked Nov 03 '22 19:11

Trevor Gehman


1 Answers

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.

like image 84
Jan Beck Avatar answered Nov 08 '22 05:11

Jan Beck