Create multiple posts when a custom post is created


I have a custom post called “Sector” and another called “Areas”. When a Sector is created, around 6 predefined Areas need to be created as well and linked with the Sector. To make this relation work, i am using Advanced Custom Fields plugin.

This is the pseudo-algorithm:

add_action('pseudo_hook', 'my_func');

function my_func($sector_id){
   $area_titles = array("Living Room", "Kitchen", "Room 1", "Room 2", "Room 3", "Bathroom 1");
   $areas = array(); 
   foreach($area_titles as $title){
      $areas[] = create_area($title=$title) // pretend it is wp_insert_post() here;
   // relate created areas to this sector
   update_field("areas", $areas, "sector_" . $sector_id) // ACF function to update metafield;

My doubts:

  1. Doing some research, i found the publish_[my_post_type] hook; but it doesn’t fit my purpose since it is triggered also when a post is edited. I need something that runs only within creation.

  2. I’d like some feedback about the performance of this approach. It won’t be used often, but certainly it will be more than 6 predefined Areas in the future.

Since i’m not an WordPress expert, i’d be very grateful for better ideas.

Thanks in advance

, , , joann2555 3 years 2020-09-04T12:10:23-05:00 0 Answers 82 views 0

Leave an answer