当编辑帖子时,我正试图找到更新自定义帖子类型slug 的方法:我需要slug 相同(清理,很明显)到标题。
使用 wpuf pro 创建和编辑自定义帖子。
我试图通过 wpuf_edit_post_after_update 挂钩更新帖子,但没有运气。
我现在在我的插件中使用此功能:
function my_set_permalink_as_title($post_id, $post) {
if ( 'my_cpt' !== $post->post_type ) return;
$title = sanitize_title_with_dashes($post->post_title);
$my_post = array(
'ID' => $post_id,
'post_name' => $title
);
wp_insert_post( $my_post );
}
add_filter( 'wpuf_edit_post_after_update', 'my_set_permalink_as_title', 10, 2 );
任何帮助表示感谢。
答案 0 :(得分:0)
我只是个白痴。
$ post对象根本不存在于我的函数中:我必须创建它。
为清楚起见,我也删除了if语句。
function my_set_permalink_as_title($post_id, $post) {
$post = get_post($post_id);
$askb_title = $post->post_title;
$askb_slug = sanitize_title($post->post_title);
$my_post = array(
'ID' => $post_id,
'post_name' => $askb_slug,
'post_type' => 'my_cpt',
'post_title' => $askb_title,
'post_content' => '',
'post_status' => 'publish',
);
wp_insert_post( $my_post );
}
add_filter( 'wpuf_edit_post_after_update' , 'my_set_permalink_as_title' , 99, 2 );