-
-
Save icemancast/88e5418ec2769bdd1d120588b23af7a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require ( ABSPATH . 'wp-admin/includes/image.php' ); | |
function worker_bee() { | |
// Delete all custom post type for events | |
$event_custom_post = get_pages( array( 'post_type' => 'company_events') ); | |
foreach( $event_custom_post as $mypost ) { | |
// Delete's each post. | |
wp_delete_post( $mypost->ID, true); | |
// Set to False if you want to send them to Trash. | |
} | |
global $wpdb; | |
$results = $wpdb->get_results( 'SELECT * FROM events_temp', OBJECT ); | |
// Match places with wordpress ones | |
$match_place = [ | |
27 => 97, | |
28 => 56, | |
13 => 122, | |
21 => 107, | |
8 => 128, | |
12 => 124, | |
2 => 139, | |
5 => 134, | |
20 => 108, | |
18 => 112, | |
25 => 101, | |
14 => 120, | |
29 => 95, | |
1 => 141, | |
17 => 114, | |
10 => 126, | |
6 => 132, | |
15 => 118, | |
7 => 130, | |
4 => 135, | |
24 => 103, | |
26 => 99, | |
23 => 105, | |
16 => 116, | |
19 => 110, | |
3 => 137 | |
]; | |
foreach($results as $result) { | |
// get temporary image table | |
$image = $wpdb->get_results( 'SELECT * FROM events_images WHERE event_id = ' . $result->id, OBJECT ); | |
$my_post = [ | |
'post_title' => htmlentities($result->name), | |
'post_name' => $result->slug, | |
'post_content' => htmlentities($result->description), | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => 'company_events' | |
]; | |
// Insert Posts | |
$post_id = wp_insert_post( $my_post ); | |
// Match places pulldown | |
$place = $match_place[$result->place_id]; | |
// Get the path to the upload directory. | |
$wp_upload_dir = wp_upload_dir(); | |
// Handle image upload and attachment | |
// https://codex.wordpress.org/Function_Reference/wp_insert_attachment | |
$filename = str_replace('img_upload/', '', $image[0]->filename); // replace img_upload/ | |
$filename = $wp_upload_dir['url'] . '/' . $filename; | |
// echo $wp_upload_dir['url'] . '/' . basename( $filename ) . '<br>'; | |
//echo $filename . '<br>'; | |
// Check filetype | |
$filetype = wp_check_filetype( basename( $filename ), null ); | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id ); | |
// Generate the metadata for the attachment, and update the database record. | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
update_field('field_559d49b0dd59c', $place, $post_id); // field_559d49b0dd59c - place | |
update_field('field_559df9072ec73', $result->place_name, $post_id); // field_559df9072ec73 - place name | |
update_field('field_559df971cd3c2', $result->place_url, $post_id); // field_559df971cd3c2 - place url | |
update_field('field_559df97ccd3c3', $result->start_date, $post_id); // field_559df97ccd3c3 - start date | |
update_field('field_55a6ceae3f3e1', $result->end_date, $post_id); // field_55a6ceae3f3e1 - end date | |
update_field('field_55a6d187b7adc', $result->recur_period, $post_id); // field_55a6d187b7adc - recur period | |
update_field('field_55a6d22c6d661', $result->recur_day, $post_id); // field_55a6d22c6d661 - recur day | |
update_field('field_55a6d2d7bd990', $result->cost, $post_id); // field_55a6d2d7bd990 - cost | |
update_field('field_55a6d2e6bd991', $attach_id, $post_id); // field_55a6d2e6bd991 - event image | |
update_field('field_55a6d2fcbd992', $image[0]->alt, $post_id); // field_55a6d2fcbd992 - event image alt | |
echo $result->name . ' -- Done<br>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment