Get post location from custom fields
-
hi there
I need the GEO my WP plugin to be able to see that a post has location data already, in a custom field
is this possible ?
thanksJoe
-
I have this question too. I’d like to pull an address to be mapped from existing custom fields – and then have the post’s pin turn up in a proximity search for the user.
This is the siteHello @aliferis and @jonburr,
This is not possible. at least not without modifying the plugin.GEO my WP saves its geolocation data in its own custom database table rather than in custom fields.
What you can do is import the current locations that you have saved in custom fields using GEO my WP importer page ( There is an option to import data from custom fields of your choice ). Then, start using GEO my WP’s Location system ( in the Edit Post page ) to geotag posts.
If you are using a custom form from the front-end to geotag posts, then you can use a custom function to sync the address submitted in the form with GEO my WP. This way a location entered via your custom form can be searchable via GEO my WP search form. The function that you can use with a couple of example can be found here.
Let me know if that helps or if you have any other questions.
-
This reply was modified 8 years, 6 months ago by
Eyal Fitoussi.
Thanks Eyal
This does help a lot
“If you are using a custom form from the front-end to geotag posts, then you can use a custom function to sync the address submitted in the form with GEO my WP. This way a location entered via your custom form can be searchable via GEO my WP search form. The function that you can use with a couple of example can be found here.”
What is the minimum location data or simplest location data I could collect from a user in order for the Geo my WP post to operate properly in proximity searches in the front end ?
I cannot expect my users to input geocoordinates but can of course ask them input address and/or zipcode information
Thanks a lot
Joe
Hi Joe,
What is the minimum location data or simplest location data I could collect from a user in order for the Geo my WP post to operate properly in proximity searches in the front end ?
GEO my WP needs to have an address and coordinates for a post in order for it to be searchable via its search forms. However, you only need to pass an address ( any form of address; like full address, zipcode, city and state and so on… ) to the function mentioned above. The function will then geocodes the address and save all the geolocation data ( address fields and coordinates ) in its custom table in database.
So, in your custom form you need to have an address field for the user to enter an address. Then, you need to hook the function above to the form submission, using a hook or a custom function, and pass the address value into the function together with the post ID. The function will then do the rest.
I hope that makes things clear.
it does – thanks very much
I will give this a go !
joe
can I pass just postcode ?
Joe
can I pass just postcode ?
Yes, you should be able to pass only zipcode.
hello again
is this still valid ?
include_once( GMW_PT_PATH .’/includes/gmw-pt-update-location.php’ );
I cannot see this file in my plugin
thanks
Joe
Sorry, I sorted the path issue I think – the file is there. I did not know the proper path
anyway, now I am testing the solution and getting partial results
when I create the post (not in the CMS) in a front end form that creates a post (not cpt)
it does not work (the postcode is being used to create GEO data in the post)
BUT when I update the post (also a front end form (same plugin)) – it works
I dotn change any data in the post. I just edit it and save it in front end and the GEO data gets setup for the post
Here is the code added to my functions file
//rename your custom function as you wish ( or leave it as is ).
function gmw_update_post_type_post_location( $post_id ) {// Return if it’s a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;// check autosave //
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) {
return;
}//check if user can edit post
if ( !current_user_can( ‘edit_post’, $post_id ) )
return;//get the address from the custom field “location”
$address = get_post_meta( $post_id, ‘location’, true );//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;//include the update location file
include_once( GMW_PT_PATH .’/includes/gmw-pt-update-location.php’ );//make sure the file included and the function exists
if ( !function_exists( ‘gmw_pt_update_location’ ) )
return;//Create the array that will pass to the function
$args = array(
‘post_id’ => $post_id, //Post Id of the post
‘address’ => $address // the address we pull from the custom field above
);//run the udpate location function
gmw_pt_update_location( $args );
}
//execute the function whenever post type is being updated
add_action( ‘save_post_post’, ‘gmw_update_post_type_post_location’ );Hi again
Further to this – I chatted to the WP User Frontend plugin guys and they recommended my using this filter – I am just not sure how to implement this in relation to the code you provided (posted earlier to this thread)
thanks again
https://docs.wedevs.com/docs/wp-user-frontend-pro/developer-docs/wpuf_add_post_after_insert/
code:
function wpufe_update_post_price( $post_id ) {
$price = get_post_meta( $post_id, ‘_price’, true );update_post_meta( $post_id, ‘_regular_price’, $price );
}add_action( ‘wpuf_add_post_after_insert’, ‘wpufe_update_post_price’ );
Hi again
an update – I seem to have two pieces of code necessary to create an integration between GEO my WP and WP User Frontend – but have been unsuccessful in combining them into 1 working piece of code in my functions file.
Here is my latest (failed) attempt
function gmw_update_post_type_post_location( $post_id ) {
// Return if it’s a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;// check autosave //
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) {
return;
}//check if user can edit post
if ( !current_user_can( ‘edit_post’, $post_id ) )
return;//get the address from the custom field “location”
$address = get_post_meta( $post_id, ‘location’, true );//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;//include the update location file
include_once( GMW_PT_PATH .’/includes/gmw-pt-update-location.php’ );//make sure the file included and the function exists
if ( !function_exists( ‘gmw_pt_update_location’ ) )
return;//Create the array that will pass to the function
$args = array(
‘post_id’ => $post_id, //Post Id of the post
‘address’ => $address // the address we pull from the custom field above
);function wpufe_update_post_geo1( $post_id ) {
//run the udpate location function
gmw_pt_update_location( $args );}
add_action( ‘wpuf_add_post_after_insert’, ‘wpufe_update_post_geo1’ );
function wpufe_update_post_geo2( $post_id ) {
//run the udpate location function
gmw_pt_update_location( $args );}
add_action( ‘wpuf_edit_post_after_update’, ‘wpufe_update_post_geo2’ );
}
//execute the function whenever post type is being updated
add_action( ‘save_post_post’, ‘gmw_update_post_type_post_location’ );-
This reply was modified 8 years, 6 months ago by
aliferis. Reason: mistakely added whole functions file not just snippet
Hi
Its working !
Here is my code for the functions file to integrate the WP User Frontend plugin add and edit post (post) with the GEO my WP plugin – to copy the value of a custom field ‘location’ to the GEO my WP fields and get the post auto Geo Coded !
function wpufe_update_post_geo1( $post_id ) {
function gmw_update_post_type_post_location( $post_id ) {
// Return if it’s a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;// check autosave //
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) {
return;
}//check if user can edit post
if ( !current_user_can( ‘edit_post’, $post_id ) )
return;//get the address from the custom field “location”
$address = get_post_meta( $post_id, ‘location’, true );//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;//include the update location file
include_once( GMW_PT_PATH .’/includes/gmw-pt-update-location.php’ );//make sure the file included and the function exists
if ( !function_exists( ‘gmw_pt_update_location’ ) )
return;//Create the array that will pass to the function
$args = array(
‘post_id’ => $post_id, //Post Id of the post
‘address’ => $address // the address we pull from the custom field above
);//run the udpate location function
gmw_pt_update_location( $args );}
gmw_update_post_type_post_location($post_id);
//execute the function whenever post type is being updated
//add_action( ‘save_post_post’, ‘gmw_update_post_type_post_location’ );}
add_action( ‘wpuf_add_post_after_insert’, ‘wpufe_update_post_geo1’ );
function wpufe_update_post_geo2( $post_id ) {
function gmw_update_post_type_post_location( $post_id ) {
// Return if it’s a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;// check autosave //
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) {
return;
}//check if user can edit post
if ( !current_user_can( ‘edit_post’, $post_id ) )
return;//get the address from the custom field “location”
$address = get_post_meta( $post_id, ‘location’, true );//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;//include the update location file
include_once( GMW_PT_PATH .’/includes/gmw-pt-update-location.php’ );//make sure the file included and the function exists
if ( !function_exists( ‘gmw_pt_update_location’ ) )
return;//Create the array that will pass to the function
$args = array(
‘post_id’ => $post_id, //Post Id of the post
‘address’ => $address // the address we pull from the custom field above
);//run the udpate location function
gmw_pt_update_location( $args );}
gmw_update_post_type_post_location($post_id);
//execute the function whenever post type is being updated
//add_action( ‘save_post_post’, ‘gmw_update_post_type_post_location’ );}
add_action( ‘wpuf_edit_post_after_update’, ‘wpufe_update_post_geo2’ );
Hi Joe,
I apologize for missing out your last replies but I am glad that you got everything working.Thank you also for sharing your solution as I am sure that others will benefit of it as well.
If you are still able to edit your last reply, perhaps you should wrap the code within the CODE tags.
Thanks,
-
This reply was modified 8 years, 6 months ago by
- The topic ‘Get post location from custom fields’ is closed to new replies.