从前端更新wordpress帖子(使用acf)
我正在制作前端管理员,供用户添加/编辑他们的帖子。我已经制作了post-add表单,它可以工作,但是编辑表单不起作用 functions.php从前端更新wordpress帖子(使用acf),wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,我正在制作前端管理员,供用户添加/编辑他们的帖子。我已经制作了post-add表单,它可以工作,但是编辑表单不起作用 functions.php function add_new_post( $post_id ) { if( $post_id == 'new' ) { // Create a new post $post = array( 'post_title' => $_POST["fields"]['field_52c8
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
// Create a new post
$post = array(
'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
'post_category' => array(4),
'post_status' => 'draft',
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
return $post_id;
}
else {
return $post_id;
}
}add_filter('acf/pre_save_post' , 'add_new_post' );
index.php
<div id="updateform-<?php the_ID(); ?>" class="collapse">
<?php
echo get_the_ID();
$args = array(
'post_id' => get_the_ID(), // post id to get field groups from and save data to
'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => get_permalink( get_the_ID() ),
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $args );
?>
</div>
要保存帖子,您应该使用保存帖子
而不是预保存帖子
。
基本上,pre\u save\u post
用于新帖子
使用下面的add_filter('save_post','add_new_post')代码>我为此创建了插件:
形成行动
将操作添加到您的ACF表单中
ACF前端显示器
在前端显示您的ACF表单如果您碰巧正在使用,您可以通过安装来实现这一点。它附带了一个ACF表单
小部件,您可以将其拖放到站点的任何位置,并通过UI配置表单(无需编码)。您有过这样的功能吗?我也在寻找解决这个问题的办法。是的,但那是很久以前的事了。现在ACF已经内置了将表单输出到前端的功能。请查收