{"id":974,"date":"2025-03-24T14:28:10","date_gmt":"2025-03-24T14:28:10","guid":{"rendered":"https:\/\/wpgeodirectory.com\/documentation\/article\/how-to-prefill-address-field-from-previous-post\/"},"modified":"2025-04-03T15:24:31","modified_gmt":"2025-04-03T14:24:31","slug":"how-to-prefill-address-field-from-previous-post","status":"publish","type":"gd_place","link":"https:\/\/wpgeodirectory.com\/documentation\/article\/code-snippets\/how-to-prefill-address-field-from-previous-post\/","title":{"rendered":"How to Prefill Address Field from Previous Post?"},"content":{"rendered":"\n<p>If you wish to prefill or prefetch the Listing Address field from the previous listing, you can use the following code snippet.&nbsp;<\/p>\n\n\n\n<p>Essentially, this code snippet will reuse the <strong>listing address <\/strong>from the previous listing.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-blockstrap-blockstrap-widget-alert d-flex align-items-center fade show alert alert-info mb-3\" role=\"alert\"><span class=\"fas fa-info-circle me-2\"><\/span><span class=\"flex-grow-1\">We recommend using the <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/useful-plugins\/code-snippets-plugin\/\">Code Snippets<\/a> plugin to add snippets to your site.&nbsp;<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Code Snippet<\/h2>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">\/**\n * For new listing, pull address data from the last post created by the user.\n *\/\nfunction gd_snippet_200522_data_from_last_post( $html, $field ) {\n    global $wpdb, $post, $gd_post;\n\n    \/\/ Check if we have a valid $gd_post and the conditions are met\n    if ( ! empty( $gd_post ) \n        &amp;&amp; empty( $gd_post-&gt;street ) \n        &amp;&amp; empty( $gd_post-&gt;latitude ) \n        &amp;&amp; empty( $gd_post-&gt;longitude ) \n        &amp;&amp; ( $user_id = get_current_user_id() ) \n        &amp;&amp; geodir_is_page( &#039;add-listing&#039; ) \n        &amp;&amp; ! empty( $gd_post-&gt;post_status ) \n        &amp;&amp; $gd_post-&gt;post_status == &#039;auto-draft&#039; \n        &amp;&amp; ! empty( $gd_post-&gt;post_type ) \n        &amp;&amp; geodir_is_gd_post_type( $gd_post-&gt;post_type ) \n        &amp;&amp; GeoDir_Post_types::supports( $gd_post-&gt;post_type, &#039;location&#039; ) ) {\n\n        \/\/ Get the correct table for the post type\n        $table = geodir_db_cpt_table( $gd_post-&gt;post_type );\n\n        \/\/ Retrieve the most recent published or pending post from the same user\n        $_gd_post = $wpdb-&gt;get_row( \n            &quot;SELECT * \n            FROM `{$table}` AS pd \n            INNER JOIN `{$wpdb-&gt;posts}` AS p ON p.ID = pd.post_id \n            WHERE ( p.post_status = &#039;publish&#039; OR p.post_status = &#039;pending&#039; ) \n            ORDER BY pd.post_id DESC \n            LIMIT 1&quot; \n        );\n\n        \/\/ If we have a valid result, update the $gd_post with address data\n        if ( ! empty( $_gd_post ) ) {\n            \/\/ Pull address data\n            $gd_post-&gt;street = $_gd_post-&gt;street;\n\n            if ( ! empty( $_gd_post-&gt;street2 ) ) {\n                $gd_post-&gt;street2 = $_gd_post-&gt;street2;\n            }\n\n            $gd_post-&gt;city = $_gd_post-&gt;city;\n            $gd_post-&gt;region = $_gd_post-&gt;region;\n            $gd_post-&gt;country = $_gd_post-&gt;country;\n            $gd_post-&gt;zip = $_gd_post-&gt;zip;\n            $gd_post-&gt;latitude = $_gd_post-&gt;latitude;\n            $gd_post-&gt;longitude = $_gd_post-&gt;longitude;\n            $gd_post-&gt;mapview = $_gd_post-&gt;mapview;\n            $gd_post-&gt;mapzoom = $_gd_post-&gt;mapzoom;\n\n            if ( ! empty( $_gd_post-&gt;neighbourhood ) ) {\n                $gd_post-&gt;neighbourhood = $_gd_post-&gt;neighbourhood;\n            }\n        }\n    }\n\n    return $html;\n}\n\n\/\/ Add filter for custom field input address\nadd_filter( &#039;geodir_custom_field_input_address&#039;, &#039;gd_snippet_200522_data_from_last_post&#039;, 9.9, 2 );\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you wish to prefill or prefetch the Listing Address field from the previous listing, you can use the following code snippet.&nbsp; Essentially, this code snippet will reuse the listing address from the previous listing.&nbsp; Code Snippet<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"article\/tags":[836,837],"article\/categories":[211,433],"class_list":["post-974","gd_place","type-gd_place","status-publish","hentry","gd_place_tags-address-field","gd_place_tags-prefill-address","gd_placecategory-tips-tricks-more","gd_placecategory-code-snippets"],"_links":{"self":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/types\/gd_place"}],"author":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/comments?post=974"}],"version-history":[{"count":0,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/974\/revisions"}],"wp:attachment":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/media?parent=974"}],"wp:term":[{"taxonomy":"gd_place_tags","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/tags?post=974"},{"taxonomy":"gd_placecategory","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/categories?post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}