How to Prefill Address Field from Previous Post?

If you wish to prefill or prefetch the Listing Address field from the previous listing, you can use the following code snippet. 

Essentially, this code snippet will reuse the listing address from the previous listing. 

We recommend using the Code Snippets plugin to add PHP snippets to your site. 

Code Snippet

/**

* For new listing pull address data from last post created by user.

*/

function gd_snippet_200522_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', 'gd_snippet_200522_data_from_last_post', 9.9, 2 )
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us