Created
March 7, 2024 02:53
-
-
Save SamuelHadsall/01a28595ab11fbb6710a8546a371c839 to your computer and use it in GitHub Desktop.
Create Post from CSV
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
class ACF_CSV_Importer { | |
private $post_type; | |
private $csv_file_path; | |
public function __construct($csv_file_path, $postType) { | |
$this->csv_file_path = $csv_file_path; | |
$this->postType = $postType; | |
} | |
public function import_from_csv() { | |
$title = ''; | |
$post_id = 0; | |
$post_data = Null; | |
$csv = array(); | |
$i = 0; | |
if (($handle = fopen($this->csv_file_path, "r")) !== FALSE) { | |
// Headrow | |
$columns = fgetcsv($handle, 4096, ',', '"'); | |
while (($row = fgetcsv($handle, 4096, ',')) !== FALSE) { | |
// This is a great trick, to get an associative row by combining the headrow with the content-rows. | |
$csv[$i] = array_combine($columns, $row); | |
switch ($postType) { | |
case 'agent': | |
$title = $csv[$i]['Agent Name']; | |
break; | |
case 'market': | |
$title = $csv[$i]['Market Name']; | |
break; | |
case 'state': | |
$title = $csv[$i]['state Name']; | |
break; | |
case 'rating-review': | |
$title = $csv[$i]['Rater First Name'] + " " + $csv[$i]['Rater Last Name']; | |
break; | |
} | |
if ($this->postType === 'agent') { | |
$post_data = array( | |
'title' => $title, | |
// Map CSV column to ACF field key | |
'field_3c908c8cedc5ce8626916d49e9c8c299' => $csv[$i]['Agent ID'], | |
'field_a0dc3f55c067b2bb192700d64b6de67d' => $csv[$i]['Market ID'], | |
'field_89faac3e84407f80ee87d4e9488f2100' => $csv[$i]['State ID'], | |
'field_7ed30e760765d80ac88202cbcd8d9f14' => $csv[$i]['BRAND'], | |
'field_1910b183c054549abba51cb53ad2ecb1' => $csv[$i]['Agent Phone Desktop'], | |
'field_ec0251b3bd9eaaedcb16accb75f43d16' => $csv[$i]['Agent Phone Mobile'], | |
'field_af182900ce928ead6fcbfbb3d26a6054' => $csv[$i]['Agent Number'], | |
'field_831691abf84b358a5df32e286a5be64c' => $csv[$i]['MSA Region'], | |
'field_d4040341ba6c44d01bfb4bb3f13b2e3a' => $csv[$i]['MSA State Abbreviation'], | |
'field_bfaa76aea2d3dd5fe0ba1e8f44c29f8d' => $csv[$i]['MSA Number'], | |
'field_a23976097f8ecc01b25051e2bfdfdf03' => $csv[$i]['Page Title Tag'], | |
'field_e5d8433bd5ea82cb02a6797a3b0a7ff4' => $csv[$i]['Page Meta Description'], | |
'field_618386ab41766761f577c9172c4173f3' => $csv[$i]['Agent Name'], | |
'field_7b85323ffa811366bab9c97c0215335b' => $csv[$i]['Agent Address 1'], | |
'field_c0489f7f12fa71e46f4eb4deb8cb368e' => $csv[$i]['Agent Address 2'], | |
'field_54d77caa5334b80593de0c00d2fd80d9' => $csv[$i]['Agent Actual City'], | |
'field_9b5095977d4d27f895351f13dff1f27a' => $csv[$i]['Agent Actual State Abbreviation'], | |
'field_fccbb620907a813559729edd5970158d' => $csv[$i]['Agent Actual Zip Code'], | |
'field_6325d508f703ee4f2fc29524ea7d51ec' => $csv[$i]['Agent Phone Number'], | |
'field_eac849a2663685bbe3df8e7fca255a9d' => $csv[$i]['Agent Website URL'], | |
'field_a70fbc42226f4886237b204215f4c0c4' => $csv[$i]['Sunday_Open'], | |
'field_7c38a55bf898f35ff9cf30646f9e7fbb' => $csv[$i]['Sunday_Close'], | |
'field_f583fd3eb7edc403c2bfbbdfa0077588' => $csv[$i]['Monday_Open'], | |
'field_bcde6c2392e8bee750df068465ac4bf9' => $csv[$i]['Monday_Close'], | |
'field_d6f1590fe52d00ec7e404731ca5bcb32' => $csv[$i]['Tuesday_Open'], | |
'field_fbd3845d7e62421678a404e7bf7ba0e5' => $csv[$i]['Tuesday_Close'], | |
'field_7618b9bb8bd3dc715821dc0d6df32208' => $csv[$i]['Wednesday_Open'], | |
'field_8182342c133523097cd9c43da938d667' => $csv[$i]['Wednesday_Close'], | |
'field_7ed4e0050f59b2bb3ec93bde39057c85' => $csv[$i]['Thursday_Open'], | |
'field_e56db465f033a53a0e733b153a78ab60' => $csv[$i]['Thursday_Close'], | |
'field_9233e4dcecbfbc7041c9a7bc0f405fa1' => $csv[$i]['Friday_Open'], | |
'field_d152f46f6aaf696398241296cdf06680' => $csv[$i]['Friday_Close'], | |
'field_7661ffb2254a061e2d576ccf9c4b7b60' => $csv[$i]['Saturday_Open'], | |
'field_caa661a7b95a44c28b4700f908793c9e' => $csv[$i]['Saturday_Close'], | |
'field_3b0eb7469ba9c95f3a05c4cef1f6aac4' => $csv[$i]['Latitude'], | |
'field_9b4dabc50f0b8ccba1c8981831abdad8' => $csv[$i]['Longitude'], | |
'field_c0b24371aa4858b898728c1055490bf8' => $csv[$i]['Hero Image'], | |
'field_0bd589a20e3c5790218dd246c1a51a9d' => $csv[$i]['Hero Title H1'], | |
'field_2f7c3030b6a8f70f70ce703fba905491' => $csv[$i]['Hero Body Text'], | |
'field_ce173dd1667eaf0f586d8783d77974f6' => $csv[$i]['Rating Supporting Copy'], | |
'field_d6f195dd224beec66fa6bb58a93c9245' => $csv[$i]['Agent Headline'], | |
'field_9ecbd31907cadfdeefbf1f69b1135b5b' => $csv[$i]['Agent Body Copy'], | |
'field_8f379de521da66a155e2852ff813d127' => $csv[$i]['Sidebar Get a Quote Title'], | |
'field_26f378fc9044d81cec037dfec467c03e' => $csv[$i]['Sidebar Get a Quote CTA Text'], | |
'field_5912e20f4a99b272aee8d14842650518' => $csv[$i]['Sidebar Get a Quote CTA Link'], | |
'field_39f293258d2c39a2b771e36c81f37642' => $csv[$i]['Moving Services Title'], | |
'field_ac3de5d79272f316b480cc30903917b1' => $csv[$i]['Moving Services Copy'], | |
'field_b7e4338a1d6884f3221cdcc15af02c9b' => $csv[$i]['Moving Services Item 1 Title'], | |
'field_58c82f9af41c1defdd5f31a277cc17f2' => $csv[$i]['Moving Services Item 1 Copy'], | |
'field_aedb8af9c2e07c10ecd436274549cef1' => $csv[$i]['Moving Services Item 1 Image'], | |
'field_769a822cf3aa4cf5759944a50c77c946' => $csv[$i]['Moving Services Item 1 CTA Text'], | |
'field_0cbd54896f345a396d4659370669ecb7' => $csv[$i]['Moving Services Item 1 CTA Link'], | |
'field_529246798cb5906f6f5ecee10cd27a1d' => $csv[$i]['Moving Services Item 2 Title'], | |
'field_de03382d9361405c4d1a92ca01bf7478' => $csv[$i]['Moving Services Item 2 Copy'], | |
'field_c52da02335f0c04867a11e84f2606bc7' => $csv[$i]['Moving Services Item 2 Image'], | |
'field_968a86a7f41a1be1bb733d248c819aea' => $csv[$i]['Moving Services Item 2 CTA Text'], | |
'field_8aa312507429f3eb4b490c1b7b911d4f' => $csv[$i]['Moving Services Item 2 CTA Link'], | |
'field_0a9172d1df5f11b64c445edf33c7fd5a' => $csv[$i]['Moving Services Item 3 Title'], | |
'field_dca2f530871af8bd507907dd4c93ba23' => $csv[$i]['Moving Services Item 3 Copy'], | |
'field_0d42bb6b5d2f05d6e4834595f2516604' => $csv[$i]['Moving Services Item 3 Image'], | |
'field_1fb3da963d4851a6e641628b55422e8b' => $csv[$i]['Moving Services Item 3 CTA Text'], | |
'field_b8240ea0275477aa1eeae2d5d3af6d09' => $csv[$i]['Moving Services Item 3 CTA Link'], | |
'field_d5afb4ad96752ea99dbf6aee6a08729a' => $csv[$i]['Moving Services Item 4 Title'], | |
'field_bbb24e82533401d4a541ebdaa7066538' => $csv[$i]['Moving Services Item 4 Copy'], | |
'field_ed5d290ff27912686390e917eb8bbdb2' => $csv[$i]['Moving Services Item 4 Image'], | |
'field_5c812f024913cb2473208941650a431a' => $csv[$i]['Moving Services Item 4 CTA Text'], | |
'field_bc8c661d488aab5ee0632dc7d63ddffc' => $csv[$i]['Moving Services Item 4 CTA Link'], | |
'field_8806121071050020c3715e08111d2e25' => $csv[$i]['Content Block 1 Title'], | |
'field_4687b15a66517fc87c12b29924927cd9' => $csv[$i]['Content Block 1 Image'], | |
'field_8be9f69d59d9e1a8c32a21122f97da65' => $csv[$i]['Content Block 1 Copy'], | |
'field_240b7d183162c3f88fa5b0dcb038f4b2' => $csv[$i]['Content Block 1 CTA Text'], | |
'field_3c731dd6b5514576ee6f44886baaa565' => $csv[$i]['Content Block 1 CTA Link'], | |
'field_5be2db5bfe21df366de5d58edd302eb6' => $csv[$i]['Content Block 2 Title'], | |
'field_663f3fc76a7d423efc6e70b3445629b1' => $csv[$i]['Content Block 2 Image'], | |
'field_72571e38ed4bd7c748a659e42082588e' => $csv[$i]['Content Block 2 Copy'], | |
'field_2754c2e87d65cff9403095ef54558250' => $csv[$i]['Content Block 2 CTA Text'], | |
'field_c61546bf9fcad9441acc43cc04399505' => $csv[$i]['Content Block 2 CTA Link'], | |
'field_90717a495e2dd79d438b109a46d4bbd8' => $csv[$i]['Long Distance Block Title'], | |
'field_a605f34d21945a5389c6fb17c1e8fad8' => $csv[$i]['Long Distance Block Eyebrow'], | |
'field_dcf3ec8942abb8c01aa5c6200fc588ff' => $csv[$i]['Long Distance Block Copy'], | |
'field_729008cd00d891498f05bbec3d126e52' => $csv[$i]['Long Distance Block CTA Text'], | |
'field_978b2f05f5f05243153d9e2e6bd88557' => $csv[$i]['Long Distance Block CTA Link'], | |
'field_2bea490bf0f4c6eacd9670fe987d3fe1' => $csv[$i]['Get a Quote Title'], | |
'field_04ec2fadc9d2151b07b8041ba72f9aa2' => $csv[$i]['Get a Quote CTA Text'], | |
'field_59c7d778758542d0d4577a63fde650e0' => $csv[$i]['Get a Quote CTA Link'], | |
// Add more fields as needed | |
); | |
} | |
foreach($post_data as $field_key => $value) { | |
if ($field_key !== 'title') { | |
// Check if data has changed | |
$post_id = $this->get_post_id_by_field($field_key, $csv[$i], $this->postType); | |
} | |
} | |
if ($post_id) { | |
$this->update_post_meta($post_id, $post_data); | |
} else { | |
$this->create_post($this->postType, $post_data); | |
} | |
$i++; | |
} | |
fclose($handle); | |
} | |
} | |
private function get_post_id_by_field($field, $value, $post_type) { | |
$args = array( | |
'post_type' => $post_type, | |
'meta_key' => $field, | |
'meta_value' => $value, | |
'posts_per_page' => 1, | |
'fields' => 'ids' | |
); | |
$posts = get_posts($args); | |
return (count($posts) > 0) ? $posts[0] : false; | |
} | |
private function create_post($post_type, $post_data) { | |
$title = $post_data['title']; | |
$post_id = wp_insert_post(array( | |
'post_title' => $title, | |
'post_type' => $post_type, | |
'post_status' => 'publish', | |
// You can add more parameters as needed | |
)); | |
if ($post_id && !is_wp_error($post_id)) { | |
// Update ACF fields | |
foreach ($post_data as $field_key => $field_value) { | |
if ($field_key !== 'title') { | |
update_field($field_key, $field_value, $post_id); | |
} | |
} | |
} | |
} | |
private function update_post_meta($post_id, $post_data) { | |
foreach ($post_data as $field_key => $field_value) { | |
$existing_value = get_field($field_key, $post_id); | |
if ($field_key !== 'title') { | |
if ($existing_value !== $field_value) { | |
update_field($field_key, $field_value, $post_id); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment