How to Prefill Address Field from Previous Post in WordPress?

/**
 * For a new listing, pull address data from the last post created by the user.
 */
function wpsc_gd_data_from_last_post($html, $field) {
    global $wpdb, $post, $gd_post;

    if (
        !empty($gd_post)
        && empty($gd_post->street)
        && empty($gd_post->latitude)
        && empty($gd_post->longitude)
        && ($user_id = get_current_user_id())
        && geodir_is_page('add-listing')
        && !empty($gd_post->post_status)
        && $gd_post->post_status == 'auto-draft'
        && !empty($gd_post->post_type)
        && geodir_is_gd_post_type($gd_post->post_type)
        && GeoDir_Post_types::supports($gd_post->post_type, 'location')
    ) {
        $table = geodir_db_cpt_table($gd_post->post_type);
        $_gd_post = $wpdb->get_row("SELECT * FROM `{$table}` AS pd INNER JOIN `{$wpdb->posts}` AS p ON p.ID = pd.post_id WHERE ( p.post_status = 'publish' OR p.post_status = 'pending' ) ORDER BY pd.post_id DESC LIMIT 1");

        if (!empty($_gd_post)) {
            // Pull address data
            $gd_post->street = $_gd_post->street;

            if (!empty($_gd_post->street2)) {
                $gd_post->street2 = $_gd_post->street2;
            }

            $gd_post->city = $_gd_post->city;
            $gd_post->region = $_gd_post->region;
            $gd_post->country = $_gd_post->country;
            $gd_post->zip = $_gd_post->zip;
            $gd_post->latitude = $_gd_post->latitude;
            $gd_post->longitude = $_gd_post->longitude;
            $gd_post->mapview = $_gd_post->mapview;
            $gd_post->mapzoom = $_gd_post->mapzoom;

            if (!empty($_gd_post->neighbourhood)) {
                $gd_post->neighbourhood = $_gd_post->neighbourhood;
            }
        }
    }

    return $html;
}

add_filter('geodir_custom_field_input_address', 'wpsc_gd_data_from_last_post', 9.9, 2);

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Share This