2012-07-30 31 views
5

में कस्टम फ़ील्ड मान के आधार पर बल्क रीराइट पोस्ट स्लग्स मूल रूप से मेरे पास वर्तमान में 5,000 से अधिक पोस्ट के साथ "पार्ट्स" नामक एक कस्टम पोस्ट प्रकार सेटअप है। "भाग संख्या" सहित प्रत्येक भाग से जुड़े कई कस्टम फ़ील्ड हैं। वर्तमान में, प्रत्येक भाग के लिए URL है:वर्डप्रेस

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

क्या मैं नहीं बल्कि होता है:

http://site.com/parts/XXXX-608-AB/ (यह एक हिस्सा संख्या, एक कस्टम फ़ील्ड "PartNo" के रूप में जमा है।)

मेरा मानना ​​है कि मुझे दो चीजें करने की ज़रूरत है:

1) कस्टम फ़ील्ड "partno" के आधार पर प्रत्येक मौजूदा भाग के लिए सभी स्लग संपादित करने के लिए एक स्क्रिप्ट बनाएं।

2) कस्टम फ़ील्ड "partno" के आधार पर नए हिस्सों के लिए हमेशा स्लग बनाने के लिए इसे ट्रिगर करने के लिए वर्डप्रेस फ़ंक्शन में हुक करें।

क्या किसी को भी इनमें से एक या दोनों पहलुओं को पूरा करने के बारे में कोई जानकारी है?

अद्यतन: नीचे कोड मैं मौजूदा पोस्ट बदलते

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

} ?> 

आप अपने विषय में यह कहीं भी बन सकता था, लेकिन मैं तो मैं आसानी से इसके साथ एक क्रॉन जॉब चला सकते हैं कि के लिए एक पृष्ठ बनाने के लिए पसंद करते हैं।

अगला हर हाल में बनाई गई पोस्ट के स्लग बदलने के लिए समारोह:

<?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 इसे ध्यान में नहीं लेता है। –

+0

ठीक है, मुझे जवाब मिला कि wp_update_post वास्तव में इसे ध्यान में रखता है। यह डुप्लिकेट के लिए post_name की जांच करेगा। –