Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8308703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:53:12+00:00 2026-06-08T18:53:12+00:00

Basically I have a custom post type setup called Parts with over 5,000 posts

  • 0

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');
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:53:13+00:00Added an answer on June 8, 2026 at 6:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have setup a custom Post Type in WordPress and have a question. How
I have a custom component called ButtonPanel written in Actionscript. Basically it's just a
I am editing a Custom Post Type template, and am using custom fields to
I have a custom server control which wraps a RadEditor (basically a textarea). I
I have a custom usercontrol, which is basically a dropdownlist, prepopulated with values from
I want to have a custom initWithNibName , basically passing in another NSString as
I have written a custom class for overriding the seek method of QSlider. Basically
I have made a custom formwizard and incorporated it into my admin interface. Basically
Basically, I have created a custom control that uses an UpdatePanel, and as I
I have a .NET assembly(3.5 framework) and it basically has a set of custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.