我已经设法做到了,我试图不使用插件开发人员建议的挂钩,但解决方案是使用挂钩。
Here is 插件的文档示例
但是我也需要编辑帖子的内容,所以我会循环查找帖子,你可以创建一个查询来获取它,也可以。所以我得到的内容如下:
<?php
$current_post = isset($_GET[\'post\'])?intval($_GET[\'post\']):\'new\';
acf_form_head();
?>
<?php get_header(); ?>
<?php
if($current_post!=\'new\')
{
?>
<?php
$query = new WP_Query( array( \'post_type\' => \'post\', \'posts_per_page\' => \'-1\', \'page_id\'=>$current_post,\'author\'=>$current_user->ID ) ); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$title = get_the_title();
$content = get_the_content();
$post_categories = wp_get_post_categories( get_the_ID() );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = $cat->cat_ID;
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php }
else
{
$title = \'\';
$content = \'\';
}
?>
然后在函数中。php我有一个过滤器,可以在提交表单时获取信息,如下所示:
function my_pre_save_post( $post_id )
{
$current_post = isset($_GET[\'post\'])?intval($_GET[\'post\']):\'\';
if($_POST[\'cat\'])
{
$cats = $_POST[\'cat\'];
foreach($cats as $catp)
{
$catobject = get_category($catp,false); // Get the Category object by the id of current category
if($catobject->category_parent); // the id of the parent category
{
array_push($_POST[\'cat\'],$catobject->category_parent);
}
}
$_POST[\'cat\']=array_unique($_POST[\'cat\']);
}
// Create a new post
$post_information = array(
\'post_title\' => wp_strip_all_tags( $_POST[\'postTitle\'] ),
\'post_content\' => $_POST[\'postContent\'],
\'post_type\' => \'post\',
\'post_status\' => \'pending\',
\'post_category\'=>$_POST[\'cat\']
);
if($current_post)
{
$post_information[\'ID\'] = $current_post;
}
// insert the post
$post_id = wp_insert_post( $post_information );
$_POST[\'return\'] = add_query_arg( array(\'post\' => $post_id), $_POST[\'return\'] );
// return the new ID
return $post_id;
}
此外,我还清除了选项表中以“new\\”开头的字段的一些选项,并在调用高级自定义字段可视化时添加此代码以将新帖子与旧帖子分开。
<?php $args = array(
\'post_id\' => $current_post ,
\'field_groups\' => array( 19 )
);
acf_form( $args ); ?>
这件事解决了我的问题,所以我想与经历过同样问题的人分享。