I am pulling some data from an API
and everything works fine except for when I try to "insert" into one of the repeater fields.
Here is a sample of the data being pulled as presented by print_r
function
Array
(
[speakers] => Array
(
[0] => Array
(
[id] => 318880
[email] => test@test39.com
[first_name] => Jody
[middle_name] =>
[last_name] => Larose
[suffix] =>
[company] => The Empire Club of Mars
[job_title] => Executive Director (Interim)
[profile_picture] => https://assets.hello.com/uploads/medium/1457077-6183b6213d030.jpg
[bio] => Jody's Bio goes Here
)
[1] => Array
(
[id] => 318878
[email] => hannah.pattison@empireclub.org
[first_name] => Hannah
[middle_name] =>
[last_name] => Pattison
[suffix] =>
[company] => Empire Club of Mars
[job_title] => Senior Manager, Events and Communications
[profile_picture] => https://assets.hello.com/uploads/medium/1288381-610d403d0b59b.png
[bio] => Lorem ipsum dolor sit amet, consectetur adipiscing elit.
)
)
[sponsors] => Array
(
[0] => Array
(
[id] => 167781
[name] => CBA
[website] => https://cba.ca/
[description] =>
[logo_id] => //assets.hello.com/uploads/full/1263290-60f57896a2296.png
[notes] =>
)
[1] => Array
(
[id] => 167783
[name] => Edelman
[website] => https://www.edelman.com/
[description] =>
[logo_id] => //assets.hello.com/uploads/full/1263300-60f5795750e25.png
[notes] =>
)
)
)
And here is an image from the how the two repeater fields are defined.
And here is the code for inserting the information from the API
function insert_events_from_api(): string {
$event_post_data = array(
'post_type' => 'event',
'post_content' => '',
'post_status' => 'publish',
);
$events = get_events_from_api();
foreach ( $events as $event ) {
if ( check_if_post_with_event_id_exists( $event['id'] ) ) {
continue;
}
$event_modified = $event + array(
'event_image' => '',
'video_link' => '',
'audio_link' => '',
);
$event_post_id = wp_insert_post(
$event_post_data + array(
'post_title' => $event['name'] ?? 'Unknown Event',
)
);
$event_keys = array_keys( $event_modified );
foreach ( $event_keys as $event_key ) {
if ( 'id' === $event_key ) {
update_field( 'event_id', $event_modified[ $event_key ], $event_post_id );
} else {
update_field( $event_key, $event_modified[ $event_key ], $event_post_id );
}
}
}
return 'EVENTS WERE INSERTED SUCCESSFULLY';
}
What I don’t understand is the event speaker data is inserted correctly but for some strange reason, the event sponsor data is not?
Here is an SQL
query am using to get the data from the db
SELECT
*
FROM
wp_postmeta
WHERE
post_id IN (
SELECT
ID
from
wp_posts
WHERE
post_type = 'event'
AND post_status <> 'trash'
)
AND meta_key LIKE '%sponsor%' OR meta_key LIKE '%speaker%
And as you can see from the screenshot, the sponsors
data looks serialized, but the speaker information is inserted correctly.
Could somebody help me out here, I don’t what am missing?