Adding API data to ACF forms

[ad_1]

Hello all, this question might be a bit specific, please feel free to direct me somewhere else if this isn’t the place to ask.

So I’m pulling data from an API and adding it to a custom Post type I created using Advanced Custom Fields (plugin). This acts as sort of a database so I can add the data to my site later.

The API I’m using is serving my data as a multi-dimensional json array. The problem is my code seems to ignore the data on anything but the first dimension of the array, so most of the data ends up not being transferred to my ACF form.

The tutorial I was using as a guide was only dealing with API that returned a single dimensional JSON. I’m wondering how I need to modify this code so that my code will read from reach dimension, basically I’d like to somehow append these chunks of the JSON together, if that makes sense.

[
{
“id”: “eca3b71919531e7ae0b4f3f501157e6c”,
“sport_key”: “americanfootball_nfl”,
“sport_title”: “NFL”,
“commence_time”: “2024-09-07T00:21:00Z”,
“home_team”: “Philadelphia Eagles”,
“away_team”: “Green Bay Packers”,
“bookmakers”: [
{
“key”: “draftkings”,
“title”: “DraftKings”,
“last_update”: “2024-05-10T13:06:17Z”,
“markets”: [
{
“key”: “h2h”,
“last_update”: “2024-05-10T13:06:17Z”,
“outcomes”: [
{
“name”: “Green Bay Packers”,
“price”: -102
},
{
“name”: “Philadelphia Eagles”,
“price”: -118
}

Example of JSON from the API, notice the “title” field for the sportsbook is on the 2nd dimension

[My ACF custom post type, notice ‘Title’ is the only field omitted](https://preview.redd.it/vh9ldp67vlzc1.png?width=548&format=png&auto=webp&s=1c4e6d703c1f14e583438afcf2a4cb1155ddcf83)

$results = wp_remote_retrieve_body(wp_remote_get( $url, $args ));

$games[] = json_decode($results);

#Add game information to the games custom post type in WP
foreach( $games[0] as $game ){

$game_slug = sanitize_title($game->home_team . ‘-‘ . $game->id);

$inserted_game = wp_insert_post([
‘post_name’ => $game_slug,
‘post_title’ => $game_slug,
‘post_type’ => ‘game’,
‘post_status’ => ‘publish’

]);

if( is_wp_error($inserted_game)){
continue;
}

$fillable = [
‘field_663519bc37440’ => ‘id’,
‘field_66351da237441’ => ‘sport_key’,
‘field_66351dd037442’ => ‘sport_title’,
‘field_66351dda37443’ => ‘commence_time’,
‘field_66351de637444’ => ‘home_team’,
‘field_66351df037445’ => ‘away_team’,
‘field_663973e7e5bcb’ => ‘title’
];

foreach( $fillable as $key => $name){
update_field( $key, $game->$name, $inserted_game);
}

}

The relevant php code. This calls the API, json decodes the data. Creates a new custom post for each game and fills out the noted fields in the post. Right now I’m only going as far as the ‘title’ field for testing.

Any help is appreciated, I understand this is a very specific question. Again, feel free to direct me somewhere else if this isn’t the right place to ask.

Thanks!

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer