I have created a custom post and fields are created by using Meta Box. Now what I am trying to achieve is:

Suppose a user enters 3 IMEI numbers, but when the user hit Publish Button, in Background there should be a loop that enters all the 3 different IMEI number as 3 different posts, where all the other meta keys are same, except the IMEI number meta key.

I just want to know the hook which can filter metadata before saving it to the database.

So far, I have tried, wp_insert_post_data hook, save_post_{$post->post_type}, rwmb_before_save_post and rwmb_after_save_post.

Is there a way through which I can achieve this?

1 Answer 1


This should be possible, but it will take some work. As you found out, at the end of the wp_insert_post function, there is a hook save_post_{$post->post_type}, which you can do to additional stuff when a post with a certain custom post type is created. However, if you use this hook to create additional posts, the hook will be encountered again and you end up in an infinite loop. So, you must make sure that the hook is not used the second time. It goes like this:

add_action ('save_post_yourcustompostname', 'wpse359582_make_three_posts');
function wpse359582_make_three_posts ($post_ID, $post, $update) {
  // remove the hook so it won't be called with upcoming calls to wp_insert_post
  remove_action ('save_post_yourcustompostname', 'wpse359582_make_three_posts');
  // make three copies
  $post1 = $post;
  $post2 = $post;
  $post3 = $post;
  // Now manipulate the three posts so each has a unique IMEI number
  // update the original post
  wp_update_post ($post1);
  // remove the id from the other two posts so WP will create new ones
  $post2['ID'] = 0;
  $post3['ID'] = 0;
  wp_insert_post ($post2);
  wp_insert_post ($post3);
  // put the hook back in place
  add_action ('save_post_yourcustompostname', 'wpse359582_make_three_posts');
  • This looks good, however, there is one issue that I am facing here. my $post object doesn't have 'meta_keys' in it. Meta keys are getting added after this hook is called. So, how to update meta_keys in between? Feb 28, 2020 at 8:04
  • For some reason they are called meta_input here. It's an array of post meta values keyed by their post meta key. Check the link to wp_insert_post. It's the last parameter.
    – cjbj
    Feb 28, 2020 at 9:35
  • that's what I am trying to say! The meta_input keys aren't getting generated while publishing the post. Is it possible that this is because I am using a plugin Meta Box to create the meta boxes? Mar 2, 2020 at 7:15
  • Possibly, I don't know how that plugin works. A meta box does not necessarily save its information as meta data. That's two different things.
    – cjbj
    Mar 2, 2020 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.