I managed to fix it like this:
register_activation_hook(__FILE__, 'activate_cpt_locations');
function activate_cpt_locations(){
$locations = [
[
'post_title' => 'Amsterdam',
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'location',
'meta_input' => [
'_location_id' => 13529,
'_sportivity_api_key' => 123456,
'_clubplanner_username' => 'user',
'_clubplanner_password' => 'pass',
],
],
[
'post_title' => 'Delft',
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'location',
'meta_input' => [
'_location_id' => 13529,
'_sportivity_api_key' => 123456,
'_clubplanner_username' => 'user',
'_clubplanner_password' => 'pass',
],
],
];
foreach($locations as $location){
wp_insert_post($location);
}
}
register_deactivation_hook(__FILE__, 'deactivate_cpt_locations');
function deactivate_cpt_locations(){
$locations = get_posts(['post_type'=>'location','numberposts'=>-1]);
foreach ($locations as $location) {
wp_delete_post( $location->ID, true );
}
}
add_action('init', 'register_location_cpt');
function register_location_cpt(){
$labels = array(
'name' => 'Location',
'singular_name' => 'Location',
'add_new' => 'Add New Location',
'add_new_item' => 'Add New Location',
'edit_item' => 'Edit Location',
'new_item' => 'New Location',
'all_items' => 'All Locations',
'view_item' => 'View Location',
'search_items' => 'Search Locations',
'not_found' => 'No Locations Found',
'not_found_in_trash' => 'No Locations found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Location',
);
//register post type
register_post_type( 'Location', array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'supports' => array( 'title', 'thumbnail','page-attributes'),
'taxonomies' => array( 'post_tag', 'category' ),
'exclude_from_search' => false,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'locations' ),
)
);
}
Is it correct that the locations are inserted in the activation hook? And that the post type is made in the init hook?