2012-07-30 17 views
5

基本上我有一個名爲「Parts」的自定義帖子類型設置,當前有超過5,000個帖子。每個零件都有許多自定義字段,包括「零件號」。目前,每個部分的網址是:根據Wordpress中的自定義字段值批量重寫帖子slu

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

我寧願是:

http://site.com/parts/XXXX-608-AB/(這是一個編號,保存爲自定義字段 「PARTNO」。)

我相信我需要做兩件事:

1)根據自定義字段「partno」製作腳本以批量編輯每個現有零件的所有段落。

2)掛入Wordpress函數來觸發它始終基於自定義字段「partno」爲新零件創建slu slu。

有沒有人對如何完成這些方面中的一個或兩個有所瞭解?

更新:下面是我最終使用改變現有員額

// 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; 
    } 
} 

更新代碼:下面是我的functions.php用來改變現有職位 (這一部分要歸功於代碼到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'); 
} 

回答

2

1)創建一個新的頁面,並分配一個新的頁面模板,它,可以說site.com/updateupdate.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'); ?> 

上面的代碼運行,每一個帖子被保存的時間(例如,當公佈的第一次),並設置一個新的POST_NAME到零件號

+0

這看起來不錯!我會嘗試現在執行。還有一個問題:是否有辦法防止意外重複?例如,如果兩部分的部件號相同,會發生什麼情況?我假設wp_update_post沒有考慮到這一點。 – 2012-08-01 21:28:51

+0

好的,我發現wp_update_post確實考慮到了這個答案。它將檢查post_name的重複項。 – 2012-08-01 22:04:04

相關問題