Hello everybody,
I found this “Display location address details” on
Display location address details
This is real cool.
Now I try to extract information and put it directly into my taxonomy LOCATION and CITY.
But how do I put the info $location[ ‘city’ ] and $location[ ‘state’ ] into the Taxonomies.
function my_copy_date_filter( $post_id ) {
$post_type = get_post_type( $post_id );
if ( $post_type != 'spot' ) {
return;
}
$location = get_field( 'spot_location', $post_id );
if( $location[ 'lat' ] && $location[ 'lng' ] ) {
update_field( 'spot_lat_lng', $location[ 'lat' ].','.$location[ 'lng' ], $post_id );
}
if( $location[ 'state' ] ) {
update_field( 'new_spot_state', $location[ 'state' ], $post_id );
}
if( $location[ 'city' ] ) {
update_field( 'new_spot_city', $location[ 'city' ], $post_id );
}
// HOW CAN I PUT $location[ 'city' ] and $location[ 'state' ] into taxonomy
}
add_filter( 'acf/save_post', 'my_copy_date_filter', 20 );
Any suggestion?
Thanks in advance,
Denis
update_field( 'new_spot_city', $location[ 'city' ], $post_id );
$post_id needs to be altered to the correct value for the taxonomy term you want to save the values to. This can be a term object or a string "term_{$term_id}"
Hello John,
thanks so far, but how does it work if I use taxonomies created with CPT UI plugin?
Also, I don’t want to show the taxonomy in the post. It should work under the hood. Will I need another fieldset?
Right now I am a little confused 😊
With your help I made it this far. But/where can I find the Term ids?
function my_copy_date_filter( $post_id ) {
$post_type = get_post_type( $post_id );
if ( $post_type != 'spot' ) {
return;
}
$location = get_field( 'spot_location', $post_id );
if( $location[ 'lat' ] && $location[ 'lng' ] ) {
update_field( 'spot_coordinates', $location[ 'lat' ].','.$location[ 'lng' ], $post_id );
}
if( $location[ 'state' ] ) {
update_field( 'spot_bundesland', $location[ 'state' ], 'term_'.$term_id );
}
if( $location[ 'city' ] ) {
update_field( 'spot_stadt', $location[ 'city' ], 'term_'.$term_id );
}
if( $location[ 'address' ] ) {
update_field( 'spot_address', $location[ 'address' ], $post_id );
}
}
add_filter( 'acf/save_post', 'my_copy_date_filter', 20 );
Greetings from Cologne,
Denis
I don’t really have enough information. You said you want to add these values to a taxonomy, so that’s what I’m going on.
But how do I put the info $location[ ‘city’ ] and $location[ ‘state’ ] into the Taxonomies.
I would need more information on what this means
Hello,
Sorry for being not clear enough.
I try again, the use only should select a spot on the google map.
Under the hood (function.php) I want extract the CITY, POSTCODE and STATE from the ARRAY and put this values in the taxonomies I created with CPT UI.
The use does only need to spot a place on the map.
Cheers,
Denis
So you want to create a taxonomy based on the location added to a post?
Then what?
Is the new term supposed to be added to the post?
How will this be used?
So you want to create a taxonomy based on the location added to a post?
Right!
Is the new term supposed to be added to the post?
Yes, the terms should be stored in the post. Term_relation or what it is called.
How will this be used?
It will be shown on the single post and so the visitor can click on it and see what other posts are in the state, city or post_code.
So, I just want to add the google Map details, to my existing taxomonies – under the hood, automatic, the User does not have to do anything.
Denis
I think I made it.
function my_copy_date_filter( $post_id ) {
$post_type = get_post_type( $post_id );
if ( $post_type != 'spot' ) {
return;
}
$location = get_field( 'spot_location', $post_id );
if( $location[ 'lat' ] && $location[ 'lng' ] ) {
update_post_meta( $post_id, 'spot_coordinates', $location[ 'lat' ].','.$location[ 'lng' ] );
}
if( $location[ 'address' ] ) {
update_post_meta( $post_id, 'spot_address', $location[ 'address' ] );
}
if( $location[ 'state' ] ) {
wp_set_object_terms($post_id, $location[ 'state' ], 'bundesland', false);
}
if( $location[ 'city' ] ) {
wp_set_object_terms($post_id, $location[ 'city' ], 'ort', false);
}
if( $location[ 'post_code' ] ) {
wp_set_object_terms($post_id, $location[ 'post_code' ], 'postleitzahl', false);
}
}
add_filter( 'acf/save_post', 'my_copy_date_filter', 20 );
It inserts new Terms related to the post 😊
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.