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?
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?
Share Improve this question edited Feb 27, 2020 at 9:03 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Feb 27, 2020 at 7:16 Shivanjali ChaurasiaShivanjali Chaurasia 283 bronze badges1 Answer
Reset to default 1This 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');
}