How to run wordpress API call and store data in advanced custom fields

Question

I have not worked with WordPress API’s before but I want to fetch data from an API and store it in the wordpress database using advanced custom fields. I have been able to do this but I manually have to go to the url:

admin-ajax.php?action=get_breweries_from_api

I want it to just run if I for example if I am on the home page. How can I do this?

This is the code in my functions.php file:

function get_breweries_from_api() {


    $current_page = ( !empty($_POST['current_page']) ) ? $_POST['current_page'] : 1;
    $breweries = [];

    $results = wp_remote_retrieve_body( wp_remote_get('https://api.openbrewerydb.org/breweries/?page=' . $current_page . '&per_page=50') );

    $results = json_decode($results);

    if ( ! is_array( $results ) || empty( $results ) ) {
        return false;
    }


    $breweries[] = $results;

    foreach ($breweries[0] as $brewery) {


        $brewery_slug = sanitize_title($brewery->name . '-' . $brewery->id);


        $existing_brewery = get_page_by_path($brewery_slug, $output, 'OBJECT', 'brewery');

        if ($existing_brewery === null) {


            $inserted_brewery = wp_insert_post([
                'post_name' => $brewery_slug,
                'post_title' => $brewery_slug,
                'post_type' => 'brewery',
                'post_status' => 'publish'
            ]);

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

            $fillable = [
                'field_5dde7801b5939' => 'name',
                'field_5dde781cb593a' => 'brewery_type',
                'field_5dde7825b593b' => 'street',
                'field_5dde782eb593c' => 'city',
                'field_5dde7835b593d' => 'state',
                'field_5dde783bb593e' => 'postal_code',
                'field_5dde7847b593f' => 'country',
                'field_5dde7851b5940' => 'longitude',
                'field_5dde785ab5941' => 'latitude',
                'field_5dde7865b5942' => 'phone',
                'field_5dde786db5943' => 'website_url',
                'field_5dde7875b5944' => 'updated_at'
            ];


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

        } else {

            // get the updated_at field from ACF and check if date from API is newer or not
            $existing_brewery_id = $existing_brewery->ID;
            $existing_brewery_timestamp = get_field('updated_at', $existing_brewery_id);

            if ($brewery->update_at >= $existing_brewery_timestamp) {
                //update post meta
              $fillable = [
                'field_5dde7801b5939' => 'name',
                'field_5dde781cb593a' => 'brewery_type',
                'field_5dde7825b593b' => 'street',
                'field_5dde782eb593c' => 'city',
                'field_5dde7835b593d' => 'state',
                'field_5dde783bb593e' => 'postal_code',
                'field_5dde7847b593f' => 'country',
                'field_5dde7851b5940' => 'longitude',
                'field_5dde785ab5941' => 'latitude',
                'field_5dde7865b5942' => 'phone',
                'field_5dde786db5943' => 'website_url',
                'field_5dde7875b5944' => 'updated_at'
            ];

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

    $current_page = $current_page + 1;
    wp_remote_post( admin_url('admin-ajax.php?action=get_breweries_from_api'), [
        'blocking' => false,
        // remove this when on live server!
        'sslverify' => false,
        'body' => [
            'current_page' => $current_page
        ]
    ] );
}

if (!wp_next_scheduled('update_brewery_list') ){
    wp_schedule_event(time(), 'hourly', 'get_breweries_from_api');
}
add_action('wp_ajax_nopriv_get_breweries_from_api', 'get_breweries_from_api');
add_action('wp_ajax_get_breweries_from_api', 'get_breweries_from_api');

And the custom post type:

function beer_post_types() {

    register_post_type('brewery', array(
        'label' => 'Breweries',
        'public' => true,
        'capability_type' => 'post'
    ));
}


add_action('init', 'beer_post_types');
0
, , , user8463989 2 years 2019-11-27T08:49:04-05:00 0 Answers 72 views 0

Leave an answer

Browse
Browse