Php Wordpress-使用作者姓名自动生成前端表单帖子标题
我使用的是高级自定义字段和自定义帖子类型ui,我需要生成一个帖子名,其中包含作者的姓名,但这只是打印“征求”,似乎我的变量$autor没有值,有什么方法可以解决这个问题吗Php Wordpress-使用作者姓名自动生成前端表单帖子标题,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我使用的是高级自定义字段和自定义帖子类型ui,我需要生成一个帖子名,其中包含作者的姓名,但这只是打印“征求”,似乎我的变量$autor没有值,有什么方法可以解决这个问题吗 function my_pre_save_post( $post_id ) { $post2 = get_post($post_id); $autor=$post2->author; // Create a new post $post = array( 'post_s
function my_pre_save_post( $post_id ) {
$post2 = get_post($post_id);
$autor=$post2->author;
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => 'Solicitud' . $autor,
'post_type' => 'solicit',
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
下面是我用来创建表单的代码,我使用的是acf_表单:
$current\u inv=$\u GET['invid'];
/**
*模板名称:征求
*/
acf_form_head();
获取_头();
?>
尝试使用“作者”而不是“作者”。在开发过程中,还要确保WP_Debug设置为TRUE
这是一个很长的版本
function my_pre_save_post( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => 'Solicitud',
'post_type' => 'solicit',
);
// insert the post
$post_id = wp_insert_post( $post );
// Once we save, retrieve the original post so we can take the post_author
$post2 = get_post($post_id);
// Use post_author
$autor = $post2->post_author;
// Update the post with the new title
wp_update_post(array('ID' => $post_id, $post2->post_title . $autor));
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
略短的版本,使用当前登录用户的用户名
function my_pre_save_post( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
$current_user = wp_get_current_user();
$author = $current_user->user_login; // OR [user_firstname, user_lastname, display_name]
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => 'Solicitud' . $author,
'post_type' => 'solicit',
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
参考资料:
function my_pre_save_post( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
$current_user = wp_get_current_user();
$author = $current_user->user_login; // OR [user_firstname, user_lastname, display_name]
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => 'Solicitud' . $author,
'post_type' => 'solicit',
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );