기본적으로 현재 사용자가 올린 게시물 유형이 "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');
}
멋진데! 나는 지금 시행하려고 할 것이다. 또 하나의 질문 : 실수로 중복되는 것을 방지 할 수있는 방법이 있습니까? 예를 들어 부품 번호가 두 부분으로 동일하면 어떻게됩니까? wp_update_post가이를 고려하지 않는다고 가정합니다. –
알았어, 나는 wp_update_post가 실제로 그것을 고려한다는 대답을 발견했다. post_name에서 중복을 검사합니다. –