2012-07-30 2 views
5

기본적으로 현재 사용자가 올린 게시물 유형이 "Parts"이며 여기에 5,000 개 이상의 게시물이 있습니다. "부품 번호"를 포함하여 각 부품과 관련된 많은 사용자 정의 필드가 있습니다. 현재 각 부분의 URL은 다음과 같습니다Wordpress에서 사용자 정의 필드 값을 기반으로 게시물 슬러그를 대량으로 다시 작성

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

내가 오히려 것 것은 :

http://site.com/parts/XXXX-608-AB/ (즉, 사용자 정의 필드 "PARTNO"로 저장 부품 번호입니다.)

1) 사용자 정의 필드 "partno"에 따라 기존 부분마다 모든 슬러그를 일괄 편집하는 스크립트를 만드십시오.

2) Wordpress 함수로 연결하여 사용자 정의 필드 "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; 
    } 
} 

UPDATE를 변경 사용하여 종료 코드는 다음과 같습니다 아래는 내가 진행하는 게시물을 일부 (감사 변경 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 및 0123를 말할 수. 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가이를 고려하지 않는다고 가정합니다. –

+0

알았어, 나는 wp_update_post가 실제로 그것을 고려한다는 대답을 발견했다. post_name에서 중복을 검사합니다. –