2

When I save my custom post_type 'product', I'd like to set their status to my custom status 'incomplete'.

  • 1
    The built-in status Draft won't do it? – Pat J Sep 9 '13 at 19:29
5

Hook wp_insert_post_data filter to force a post status to be set as incomplete before it can be set as published. With the following code only a post that is set as incomplete can be saved:

add_filter( 'wp_insert_post_data', 'prevent_post_change', 20, 2 );

function prevent_post_change( $data, $postarr ) {
    if ( ! isset($postarr['ID']) || ! $postarr['ID'] ) return $data;
    if ( $postarr['post_type'] !== 'product' ) return $data; // only for products
    $old = get_post($postarr['ID']); // the post before update
    if (
        $old->post_status !== 'incomplete' &&
        $old->post_status !== 'trash' && // without this post restoring from trash fail
        $data['post_status'] === 'publish' 
    ) {
        // set post to incomplete before being published
        $data['post_status'] = 'incomplete';
    }
    return $data;
}
  • this is the right solution ! just wondering, why do you put "20, 2" when they say "99, 2" in the Codex ? codex.wordpress.org/Plugin_API/Filter_Reference/… – Vincent Wasteels Sep 10 '13 at 3:18
  • That's the priority. An higher level means the filter run later, a lower means a filter run first. This as effect only if there are more than 1 filter to the same hook. 20 is the standard priority for all hooks. So whathever is the number you use the filter works, if you think that some other filter can change what your filter do you can use an higher priority, but using '99' cannot assure you filter runs the last. If another use '999'... you can use '999', another filter can use '9999', and so on. I usually use the standard 20 and if there are problems I look for it and change the priority. – gmazzap Sep 10 '13 at 7:16
  • @VincentWasteels, I forgot to tag you in previous comment. – gmazzap Sep 10 '13 at 7:29
  • Simple but perfect, I had been trying for hours to find a solution but without success. – Lenin Zapata Jul 9 '18 at 6:43

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.