我使用高级自定义字段插件和自定义帖子类型UI来给我的用户一些额外的功能。我的问题是,我已经设置了一个用户信息菜单,并在列表视图中所有新帖子显示为自动起草。无论如何,我可以使字段鼻涕虫公司名称作为列表视图的帖子标题?
我尝试了下面的代码,但它没有更新公司名称作为帖子标题,并且在自定义帖子页面中显示消息,如"您目前正在编辑显示您最新帖子的页面。"
我的示例代码:
add_filter('title_save_pre', 'save_title');
function save_title() {
if ($_POST['post_type'] == 'users') : // my custom post type name
$new_title = $_POST['company_name']; // my custom field name
$my_post_title = $new_title;
endif;
return $my_post_title;
}
应该可以:
add_action( 'acf/save_post', 'save_post_handler' , 20 );
function save_post_handler( $post_id ) {
if ( get_post_type( $post_id ) == 'users' ) {
$title = get_field( 'company_name', $post_id );
$data['post_title'] = $title;
$data['post_name'] = sanitize_title( $title );
wp_update_post( $data );
}
}
在输入中使用name="post_title"
<input type="text" name="post_title" id="meta-text" class="form-control" value="">
免责声明:我昨天从上到下阅读了php网站,但是我读了一些关于尝试这样做的帖子,并组装了这个为我工作的解决方案。我有一个自定义的帖子类型叫做artists,我将first_name和last_name的artist acf字段组合起来,并将其设置为标题。对于您的示例,您可以删除添加姓氏的部分。
// Auto-populate artist post type title with ACF first name last name.
function nd_update_postdata( $value, $post_id, $field ) {
// If this isn't an 'artists' post type, don't update it.
if ( get_post_type( $post_id ) == 'artists' ) {
$first_name = get_field('first_name', $post_id);
$last_name = get_field('last_name', $post_id);
$title = $first_name . ' ' . $last_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'artists',
'post_name' => $slug
);
wp_update_post( $postdata, true );
return $value;
}
}
add_filter('acf/update_value/name=first_name', 'nd_update_postdata',
10, 3);
add_filter('acf/update_value/name=last_name', 'nd_update_postdata', 10,
3);