1) 创建一个新页面并为其分配一个新的页面模板,比如站点。com/更新和更新。php。更新内部。php write you批量机制:
<?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作业。
接下来是更改每个新创建帖子的slug的函数:
<?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\\u名称设置为零件号。