0

I have a problem with front end update on post + advanced custom fields plugin it does not clear the input on new post. It does insert or update the form correctly but when you got there for first time you see the last input things in the fields from the acf plugin. I have search for the problem and i have found one post which suggest to properly filter the $post_id i have try that too.

<?php
acf_form_head(); 

if ( is_user_logged_in() ) { 
$current_post = isset($_GET['post'])?intval($_GET['post']):'';

if($_POST)
{
$post_information = array(
'post_title' =>  wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'pending'
);
    if($current_post)
    {
        $post_information['ID'] = $current_post;
    }
$post_id = wp_insert_post( $post_information );
$_POST['return'] = add_query_arg( array('post' => $post_id), $_POST['return'] );
//print_r($current_post);
//exit();
}
?>
<?php get_header(); ?>
<?php
if($current_post)
    {
?>
<?php 
  $query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => '-1','page_id'=>$current_post ) ); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
    $title = get_the_title();
    $content = get_the_content()    ?>

<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php }
else
{
    $title = '';
    $content = '';
}
 ?>
<main role="main">
<!-- section -->

  <section>
<form action="" id="primaryPostForm" method="POST">
  <fieldset>
    <label for="postTitle">
      <?php _e( 'Post\'s Title:', 'framework' ); ?>
    </label>
    <input type="text" name="postTitle" id="postTitle" value="<?php echo $title; ?>" class="required" />
  </fieldset>
  <?php if ( $postTitleError != '' ) { ?>
  <span class="error"><?php echo $postTitleError; ?></span>
  <div class="clearfix"></div>
  <?php } ?>
  <fieldset>
    <label for="postContent">
      <?php _e( 'Post\'s Content:', 'framework' ); ?>
    </label>
    <textarea name="postContent" id="postContent" rows="8" cols="30"><?php echo $content; ?></textarea>
  </fieldset>
  <fieldset>
  <?php wp_dropdown_categories(""); ?>
    <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
    <input type="hidden" name="submitted" id="submitted" value="true" />
    <?php $args = array(
            /*'post_id' => 'new_post',*/
            'field_groups' => array( 19 )
        );

        acf_form( $args );  ?>
  </fieldset>
</form>

0

I have managed it to work i was trying not to work on with hooks like the plugin developers suggest, but the solution was with hook.

Here is the documentation example of the plugin

But i need to edit the content of the post too so i make a loop to find the post you can create a query to get it too it will be ok too. So i get the content like this:

<?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 = '';
    }
?>

And then in the function.php i have a filter which get the information when the form is submitted like this:

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;
}

Also i clear some options in the options table with fields starting with "new_" and add this code to separate the new post from the old when call the advanced custom fields visualization.

<?php $args = array(
                'post_id' => $current_post ,
                'field_groups' => array( 19 )
            );

            acf_form( $args );  ?>

This things resolve my problem so i wanted to share with someone which experience the same problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.