I want to add custom validation to post(custom post type) before it save into the WordPress. I am using the Rest API to insert the post to custom post type. What I need is the fields which I created using ACF(Advance Custom Fields) should be validated before it actually save into the WordPress Backend. I tried to find the hook action for before save but no luck so far. Quick help will be appreciate.

  • Thanks for the quick answer. But When I call the API using the POST method for storing the data into custom post type. It's not validates the fields which I checked as required in ACF. – Jenil Kanani Dec 12 '18 at 13:11
  • Hi @vikrantzilpe The above functions not worked for me for custom post type. Do you have any work around here? – Jenil Kanani Dec 12 '18 at 13:16
  • Thanks for look into this but above link is only guide me how to create custom post type. – Jenil Kanani Dec 12 '18 at 13:41
  • @vikrantzilpe Do you find any hooks actions that runs before saving the posts where I can validate each fields that passed into rest api? – Jenil Kanani Dec 12 '18 at 13:42
  • add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0); – vikrant zilpe Dec 12 '18 at 13:44

By searching a lot to API documentation, I found the solution to my problem finally:

function my_rest_prepare_post(  $prepared_post, $request ) {


add_filter( 'rest_pre_insert_posttype', 'my_rest_prepare_post', 10, 3 );
  • Just an observation, it should be 2 instead of 3. The filter only takes 2 arguments, this is the core code: return apply_filters( "rest_pre_insert_{$this->post_type}", $prepared_post, $request );. And a hint for those looking onto this filter, we should return a WP_Error to abort the item insertion. – brasofilo Nov 10 '20 at 21:13

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.