Support

Account

Home Forums Front-end Issues Change Post Status on Edit

Solved

Change Post Status on Edit

  • Is there a way to force the status of a post if it is edited, in the way that a new post can be saved as ‘pending’, can the same be done every time a post is edited using a front end form?

  • You can use the acf/pre-save-post hook to create a filter that can do this. Most of the details can be found here http://www.advancedcustomfields.com/resources/acf-pre_save_post/

  • Hi John,

    Thanks for your response, but the linked code appears to be only for New Posts again?

    How would I adjust this for existing posts?

    Many Thanks,
    Ashley

  • This filter can also be used for other purposes, for example changing the post status for a post would look something like this

    
    <?php 
    
      function my_pre_save_post($post_id) {
      
        // check for numerical post_id and check post_type
        if (!is_numeric($post_id) || get_post_type($post_id) != 'post') {
          return $post_id;
        }
    
        // update status to draft
        $post = array(
          'post_status'  => 'draft' ,
        );  
    
        // update the post
        $post_id = wp_insert_post($post); 
        wp_update_post($post);
        
        return $post_id;
      }
      
      add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
      
    ?>
    
  • That worked perfectly!

    Thanks for all your help.

  • Hi John,

    Unfortunately I have since discovered that although the post status is changed correctly, none of the changes made to the form itself are saved?

    Any idea why this would be?

    Kind regards,
    Ashley

  • Looking back at the code, I think it may have something to do with the call to update post, not sure why that’s in there, either that of the insert post. Looking at it, since we’re updating the post the the insert needs to be removed and an change is needed to the update.

    try this.

    
    <?php 
    
      function my_pre_save_post($post_id) {
      
        // check for numerical post_id and check post_type
        if (!is_numeric($post_id) || get_post_type($post_id) != 'post') {
          return $post_id;
        }
    
        // update status to draft
        $post = array(
          'ID' => $post_id,
          'post_status'  => 'draft' ,
        );  
    
        // update the post
        wp_update_post($post);
        
        return $post_id;
      }
      
      add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
      
    ?>
    
  • Hi John,

    Many thanks for this, the post is updating as expected now!

  • This doesn’t work for the scenario where you have an ‘add post’ form and an ‘update post’ form. What happens is when you add a new post with the status ‘draft’ it will override that too. I assume the use case for this is a validation workflow i.e. a user adds a post which is set to draft, then somewhere else somebody checks the post to validate it.

  • @aaron91brimacombe change the priority of the filter to 1 so that it runs before the built in ACF filter.

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

The topic ‘Change Post Status on Edit’ is closed to new replies.