Created
August 3, 2016 15:43
-
-
Save shaunsantacruz/2e6031103e5e5e238eb26ca0d7cab064 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| /** | |
| * Plugin Name: ACF to CPT with Taxonony Terms | |
| * Description: | |
| */ | |
| register_activation_hook(__FILE__, function () { | |
| global $wpdb; | |
| $i = 0; | |
| while ( $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key LIKE '%trainings\_{$i}\_%'") ) { | |
| // replace trainings with whatever you named it | |
| $title = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'trainings_{$i}_location_name'"); | |
| $trainer = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'trainings_{$i}_trainer_name'"); | |
| $state = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'trainings_{$i}_state'"); | |
| //var_dump($title); | |
| //var_dump($trainer); | |
| //var_dump($state); | |
| $post_id = wp_insert_post([ | |
| 'post_type' => 'training', | |
| 'post_title' => $title, | |
| 'post_content' => '', | |
| 'post_status' => 'publish', | |
| 'comment_status' => 'closed', | |
| 'ping_status' => 'closed', | |
| ]); | |
| if ( $post_id ): | |
| // Foriegn key update: post_id | |
| $wpdb->query("UPDATE $wpdb->postmeta SET post_id = {$post_id} WHERE meta_key LIKE '%trainings\_{$i}\_%'"); | |
| // set repeater index keys to 0 for this post | |
| // e.g. trainings_15_location_name to trainings_0_location_name | |
| $query = " | |
| UPDATE $wpdb->postmeta | |
| SET meta_key = REPLACE(meta_key, 'trainings_{$i}_', 'trainings_0_') | |
| WHERE meta_key | |
| LIKE '%trainings\_{$i}\_%'"; | |
| $wpdb->query($query); | |
| // insert acf repeater meta data | |
| $data = [ | |
| 'meta_key' => 'trainings', | |
| 'meta_value' => 1, | |
| 'post_id' => $post_id, | |
| ]; | |
| $wpdb->insert($wpdb->postmeta, $data); | |
| $_data = [ | |
| 'meta_key' => '_trainings', | |
| 'meta_value' => 'field_54ff74fde4d31', | |
| 'post_id' => $post_id, | |
| ]; | |
| $wpdb->insert($wpdb->postmeta, $_data); | |
| // relate term & taxonomy to a post | |
| if ( $state ) { | |
| wp_set_object_terms($post_id, $state, 'state'); | |
| } | |
| if ( $trainer ) { | |
| wp_set_object_terms($post_id, $trainer, 'trainer'); | |
| } | |
| endif; | |
| $i ++; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment