Support

Account

Home Forums ACF PRO Save edited post as pending / add acf field title

Solved

Save edited post as pending / add acf field title

  • I have 2 issues, which I think are related.

    I have an acf_form which is displayed through a shortcode on a single page.
    So the post id which is returned is from the page on which it’s shown, not the id of the post.
    But I realised this and made a work around for it, since my post edit page is like this http://www.domain.com/edit/?ad=post_id

    This all works as expected for submitting and updating posts, except for 2 things:
    1) I can’t seem to add/update the post title from an ACF field
    2) I can’t set the status of a post to ‘pending’ after user submits

    AFAIK I can only set a post_status for new posts (in acf_form) in the new_post array.

    I tried to override the post status (and post title from an ACF field) with the use of this topic, but that unfortunately didn’t work.

    I also tried the solution from this topic, but also no success.

    I also looked at this topic but again no cigar….

    This is what I have now, but I can’t get any of the functions to work.

  • Hi @beee

    You can show the post title field to allow for creating/editing that using post_title => true in acf_form().

    To change the post status after a submission of updating an existing post you should use the acf/save_post action hook. You could update it using wp_update_post() and if you do you HAVE TO remove your acf/save_post action before and add it back afterwards. Otherwise you’ll end up with an infinite loop.

    Another way is to change it using the global $wpdb variable directly.

    
    global $wpdb;
    //Skip if this isn't a post or if status is already pending.
    if ( ! $post = get_post( $post_id ) ) return;
    if ( 'pending' == $post->post_status ) return;
    //Update DB with pending status.
    $wpdb->update( $wpdb->posts, array( 'post_status' => 'pending' ), array( 'ID' => $post_id ) );
    //clean the cache for the post
    clean_post_cache( $post->ID );
    
    //Trigger all transition hooks in wordpress (in case other functions hook into those to do things.
    $old_status = $post->post_status;
    $post->post_status = 'pending';
    wp_transition_post_status( 'publish', $old_status, $post );
    
  • Hi Jonathan,
    Thanks for the answer.

    I know about post_title => true but I don’t like it because I prefer to have it IN the form, instead of above it.

    I don’t quite understand what you mean with remove the acf/save_post hook. The functions I mentioned are not in use right now (the file in which they reside is not included right now because I couldn’t get them to work), so is there something to remove ?

    I hooked your function into acf/save_post and it works like a charm.
    I added a bit of code myself to also update the post title simultaneously.
    This is the final working result but I need the remove/add actions here ?

  • I then tried to repeat something similar to ADD the post title upon first submit from the same acf field with the help of this explanation.

    After testing with exits, I noticed new_post is never the variable so it never reaches the rest of the code.

    I echoed the post_id with an exit (before line 10) and it always returned an ID, so the script returns the ID on line 10 and never passes that.

    When I commented out the return, I got 2 new posts instead of one. One did now actually have the title as I wanted it but the second one is also inserted.

    So I removed the insert and added an update to change the post title and name.

    Can you take a look at this function and see if all looks good ?

    It appears to work as expected and all, but would like to make sure it’s all on the up and up.

  • Hi @beee

    There’s some different ways to go about it. Since you’re using the pre_save_post hook you could just as easily change the title acf field value straight in the $_POST.

    You would only need to remove the save_post action hook IF you’re doing a function that itself would call to the same action (like wp_insert_post or wp_update_post). That would result in an infinite loop. However you’re not using any of those so you’re fine.

    Basically.. your function looks fine and it works. But it might be better practice to not use the $wpdb object. One thing you need to change tho if you continue to use this is the prepare function for $wpdb. Since you’re inserting information from variables (in the front end none the less) you should really make sure to prepare them even tho you’re sanitizing the value before insertion. https://developer.wordpress.org/reference/classes/wpdb/prepare/

  • Right now I’m planning to use this since it’s working πŸ˜‰ The only variable I use is a post ID, so I think I’m fairly save.

    How would you recommend to do it ?

    The only thing I can’t achieve yet is strip special characters from an ACF field.
    I can strip them from the field and insert the stripped title into post_title but back in to the ACF field gives me an empty ACF field and title.

  • Just use what you’re using πŸ™‚
    You can use strip_tags() to avoid tags

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

You must be logged in to reply to this topic.