WordPress.org

Ready to get started?Download WordPress

Forums

wp_insert_post not replicating standard admin 'new post' behaviour (2 posts)

  1. t22james
    Member
    Posted 3 months ago #

    Hi there,
    I'm trying to implement some code using Advanced custom fields, to post rich posts (as in, with custom fields) from the front end. To add the new post, the code I have uses:

    // Create a new post
          $post = array(
              // 'post_status'  => 'draft' ,
              'post_title'  => $_POST["post_title"],
              'post_content' => $_POST["post_content"],
              'post_type'  => 'post' ,
              'post_status' => 'publish',
          );  
    
          // insert the post
          $post_id = wp_insert_post( $post ); 
    
          // update $_POST['return']
          $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );

    I've noticed however, that this only creates a single/the 'original' post entry into the posts database table. In contrast, when I post the same post type from the wp-admin backend, a new post will enter not only the 'original' post into the database, but also seems to immediately create a revision entry also. The effect of this seems to be that if I update one of my frontend posts, the first incarnation is lost as the 'original' post is updated to match the latest revision, whereas in the wp-admin post, the originally created revision will still hold the very first incarnation of the post.

    So, how can I replicate that behaviour on my front-end form?

  2. bcworkz
    Member
    Posted 3 months ago #

    You would probably need to submit your front end form to post.php exactly as the back end edit form would. This would mean matching all the field names and creating a nonce in a hidden field using the same exact string as is used to populate the hidden field on post-new.php.

Reply

You must log in to post.

About this Topic