Support

Account

Home Forums Front-end Issues Allow 'save to draft' and 'publish' on front end form

Solved

Allow 'save to draft' and 'publish' on front end form

  • Hiya

    So I have a client who’s requiring a front-end form that allows for 3 different ways of adding a new post:

    http://d.pr/i/Z3NE <– image here

    1) Add another – pretty sure i can do this by just setting the return URL to be back to this form. No problem here.

    2) Save for later – this basically requires that the new_post array has a post_status of draft, instead of publish

    3) Standard functionality as per new_post array.

    Is there a way for me to hook up the ‘save for draft’ button so it does just that?

  • Using acf_form() what you’ll need to do is create something like a radio button field and then add a custom acf/pre_save_post filter https://www.advancedcustomfields.com/resources/acf-pre_save_post/ and set the status of the post in the filter.

  • Genius @hube2!

    Thanks for the point in the right direction, I’ll give this a go.

    Side question: When putting unique data into a filter like this, does all the other information from the form – fields, content, title etc – still get set, in _addition_ to whatever I specify/override in my filter?

  • If you keep everything in acf_form() the same, the acf built in pre_save_post filter is run. The only thing you should need to add to your filter are those things you want to do differently.

  • @hube2 I tried the above suggestion but post_status is not being set correctly – it still sets the post as published.

    function my_pre_save_post( $post_id ) {
    
      // check if this is to be a new post
      if( $post_id != 'new_post' ) {
        return $post_id;
      }
    
      // Create a new post
      $post = array(
        'post_status'  => $_POST['acf']['field_58aa9af6e90cd'],
      );
    
      // insert the post
      $post_id = wp_insert_post( $post ); 
    
      // return the new ID
      return $post_id;
    
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

    In my case, the field field_58aa9af6e90cd returns either ‘draft’ or ‘publish’

    Am I missing something?

  • Your post id will not be equal to “new_post”. ACF should have already inserted the new post.

    What you need to do is check the post type that was inserted with get_post_type() and then update the post that was already inserted using wp_update_post() https://codex.wordpress.org/Function_Reference/wp_update_post

  • Ah ok, so I’m essentially creating the new post, and then updating it’s status etc.

    Got it tx.
    Will give that a go.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.