基于Wordpress中的自定义字段值大容量重写post-stugs


Bulk rewrite post slugs based on custom field value in Wordpress

基本上,我有一个名为"Parts"的自定义帖子类型设置,其中目前有5000多个帖子。每个部分都有许多自定义字段,包括一个"part number"。目前,每个部分的URL是:

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

我更想要的是:

http://site.com/parts/XXXX-608-AB/(这是一个零件号,存储为自定义字段"partno"。)

我认为我需要做两件事:

1) 根据自定义字段"partno",制作一个脚本来批量编辑每个现有零件的所有嵌段。

2) 勾入Wordpress函数,以触发它始终基于自定义字段"partno"为新零件创建slug。

有人知道如何完成这两个方面中的一个或两个吗?

更新:以下是我最终用于更改现有帖子的代码

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

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设置为零件号