Google Places API Data to Custom Fields

Support MB Custom Post Type Google Places API Data to Custom Fields

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #32216
    Alperen OzkanAlperen Ozkan
    Participant

    I run the code for the normal posts and it retrieves the data from google places. But I want the data to be written to a CPT.

    I am changing add_action parameters like save_post_CPT-slug but this hook is not working. Should I make a setting on the MetaBox side?

    
    function google_places_data( $post = null ) {
        if ( ! $post = get_post( $post ) )
            return;
        if ( ! $place_id = get_post_meta( $post->ID, 'place_id', true ) )
            return; // No place ID configured for this post
    
    if ( $data = get_post_meta( $post->ID, 'place_data', true ) ) {
        if ( $data->timeout <= current_time( 'timestamp' ) )
            $data = null; // Void the cache if it's old
    }
    
    if ( ! $data ) {
        $args = http_build_query(
            array(
                'key' => 'AIzaSyCarm54WzOXFhXqmEU3rkUorDoa8b3Nzog', // API key
                'placeid' => $place_id
                )
            );
    
    $http = wp_remote_get( "https://maps.googleapis.com/maps/api/place/details/json?$args" );
    if ( $body = wp_remote_retrieve_body( $http ) ) {
        if ( $data =@ json_decode( $body ) )
            $data->timeout = current_time( 'timestamp' ) + HOUR_IN_SECONDS; // Cache data for 1 hour
    
        $place_address = $data->result->formatted_address; 
        $place_lat = $data->result->geometry->location->lat;
        $place_long = $data->result->geometry->location->lng; 
        $place_phone = '000-000-010';
    
        update_post_meta( $post->ID, 'place_address', $place_address );
        update_post_meta( $post->ID, 'place_lat', $place_lat );
        update_post_meta( $post->ID, 'place_long', $place_long );
        update_post_meta( $post->ID, 'place_phone', $place_phone );
    
    }
    
    if ( $data ) {
        update_post_meta( $post->ID, 'place_data', $data );
    
    } else {
        echo 'api error';
    }
    
    }
    
    return $data;
    }
    add_action( 'save_post', 'google_places_data', 10, 4 );
    #32231
    Long NguyenLong Nguyen
    Moderator

    Hi,

    We have the extension MB Geolocation which supports populating location data (address, lat, long, city ...) to the custom fields easily. Please read more on the documentation https://docs.metabox.io/extensions/meta-box-geolocation/

    #32299
    Alperen OzkanAlperen Ozkan
    Participant

    Hi Long,
    I am trying to get not only location data but also place details. I have fixed my code like below, but I really appreciate it if you can help me to save this data to a custom table which I have already created with metabox custom table extension.

    
    <?php
    
    add_action( 'save_post', 'google_places_data', 10, 4 );
    
    function google_places_data( $post = null ) {
        global $post;
        if($post->post_type == 'hamburg'){
            
        if ( ! $post = get_post( $post ) )
            return;
        if ( ! $place_id = get_post_meta( $post->ID, 'place_id', true ) )
            return; // No place ID configured for this post
    
        if ( $data = get_post_meta( $post->ID, 'place_data', true ) ) {
            if ( $data->timeout <= current_time( 'timestamp' ) )
                $data = null; // Void the cache if it's old
        }
    
        if ( ! $data ) {
            $args = http_build_query(
                array(
                    'key' => 'MY API KEY', // API key
                    'place_id' => $place_id
                    )
                );
    
            $http = wp_remote_get( "https://maps.googleapis.com/maps/api/place/details/json?$args" );
            if ( $body = wp_remote_retrieve_body( $http ) ) {
                if ( $data =@ json_decode( $body ) )
                    $data->timeout = current_time( 'timestamp' ); // Cache data for 1 hour
                    
                $address_components_length = count($data->result->address_components);
                
                $bezirk; $city; $state; $post_code;
                foreach ($data->result->address_components as $component_data){
                    $type = $component_data->types[0];
                    if ($type == 'sublocality_level_1') {
                        $bezirk = $component_data->long_name;
                    } else if ($type == 'locality') {
                        $city = $component_data->long_name;
                    } else if ($type == 'administrative_area_level_1') {
                        $state = $component_data->long_name;
                    } else if ($type == 'postal_code') {
                        $post_code = $component_data->long_name;
                    }
                    
                }
                
    
                $name = $data->result->name;
                $formatted_address = $data->result->formatted_address;
                $formatted_phone_number = $data->result->formatted_phone_number;
                $rating = $data->result->rating;
                $total_rating = $data->result->user_ratings_total;
                $google_maps_url = $data->result->url;
                $website = $data->result->website;
                
                update_post_meta( $post->ID, 'name', $name );
                update_post_meta( $post->ID, 'formatted_address', $formatted_address );
                update_post_meta( $post->ID, 'formatted_phone_number', $formatted_phone_number );
                update_post_meta( $post->ID, 'bezirk', $bezirk );
                update_post_meta( $post->ID, 'city', $city );
                update_post_meta( $post->ID, 'state', $state );
                update_post_meta( $post->ID, 'post_code', $post_code );
                update_post_meta( $post->ID, 'rating', $rating );
                update_post_meta( $post->ID, 'total_rating', $total_rating );
                update_post_meta( $post->ID, 'google_maps_url', $google_maps_url );
                update_post_meta( $post->ID, 'website', $website );
    
            }
    
            if ( $data ) {
                update_post_meta( $post->ID, 'place_data', $data );
                
            } else {
                echo 'api error';
            }
    
        }
        return $data;
        }
    }
    
    #32310
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you've created a custom table before, please follow this documentation to use the public API to save the field value to the custom table.
    https://docs.metabox.io/extensions/mb-custom-table/#api

    #33633
    shmaltzshmaltz
    Participant

    @Long Nguyen It would be really awesome if you can integrate this into the Geolocation extension, because it is already using the Places API.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.