2012-07-30 12 views
5

Fondamentalmente ho un setup personalizzato del tipo di post chiamato "Parts" con oltre 5.000 post al momento in esso. Vi sono numerosi campi personalizzati associati a ciascuna parte, incluso un "numero di parte". Attualmente, l'URL di ogni parte è:Riscrivo in blocco di lumache post in base al valore del campo personalizzato in Wordpress

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

Cosa avrei preferito è:

http://site.com/parts/XXXX-608-AB/ (che è un numero di parte, memorizzato come un campo personalizzato "PartNo".)

Credo di dover fare due cose:

1) Creare uno script per modificare in blocco tutti gli slug per ogni parte esistente, in base al campo personalizzato "partno".

2) Collegarsi a una funzione di Wordpress per attivarla per creare sempre lo slug per le nuove parti in base al campo personalizzato "partno".

Qualcuno ha qualche conoscenza su come realizzare uno o entrambi questi aspetti?

UPDATE: Di seguito è riportato il codice ho finito per usare per modificare i messaggi esistenti

// 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: Di seguito è riportato il codice che ho usato in functions.php di modificare i messaggi in corso (grazie in parte a 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'); 
} 

risposta

2

1) creare una nuova pagina e assegnare un nuovo modello di pagina ad esso, consente di dire site.com/update e update.php. All'interno di update.php si scrive meccanismo di massa:

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

} ?> 

Si potrebbe mettere questo ovunque nel tema, ma mi piace creare una pagina per questo in modo da poter facilmente eseguire un job cron con esso.

successivo la funzione di modificare la lumaca di ogni nuova funzione:

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

Il codice di cui sopra viene eseguito ogni volta che un post viene salvato (ad esempio, quando è stato pubblicato per la prima volta) e stabilisce un nuovo POST_NAME al parte n.

+0

Questo sembra fantastico! Cercherò di implementare ora. Un'altra domanda: esiste un modo per prevenire la duplicazione accidentale? Ad esempio, cosa succede se il codice è lo stesso per due parti? Presumo che wp_update_post non ne tenga conto. –

+0

Ok, ho scoperto la risposta che wp_update_post ha effettivamente preso in considerazione. Controllerà il post_name per i duplicati. –

Problemi correlati