Open kmmathis opened 12 years ago
Yes, this would be helpful.
Same here. Any way to do this?
I agree that it would be helpful for there to be a function to do this built into the plugin. That said, if you need to retrieve those fields, you can do so like this (where 'location_address' is the field name you assigned in ACF):
$address = get_post_meta($post_object->ID, 'location_address');
That will return an array of the address values and you can then do something like assigning variables to the individual items:
$address1 = $address[0]['address1'];
$address2 = $address[0]['address2'];
$city = $address[0]['city'];
$state = $address[0]['state'];
$postal_code = $address[0]['postal_code'];
If you allow someone to do a repeater field, you'll have multiple items in the array so you'll need to do a foreach loop:
$addresses = get_post_meta($post_object->ID, 'location_address');
foreach($addresses as $address) {
echo $address['address1']." ".$address['address2']."<br>".$address['city'].", ".$address['state']." ".$address[0]['postal_code'];
}
Curious - is there a way of setting values programmatically? ie http://www.advancedcustomfields.com/docs/functions/update_field/
It seems that I can create an object/array of key/value pairs and then use that for the update_field('address', obj, ID)?
@indianabenny Thanks man. This is exactly what I needed.
@justintucker Glad it was helpful.
@indianabenny is your code still valid? I've been trying it and it's not rendering anything.
@industriousind If you are using this code, it should work fine if you are using this plugin with the pre-4.0 version of ACF. Can you post up an example of the code where you are using my code sample above?
FYI, if you need a post 4.0 version of this plugin, you can find one here: https://github.com/strickdj/acf-field-address
@indianabenny I am using v4.0, ACF is up-to-date and so is Wordpress. What I'm trying to do is make the address link to maps, using Apple's iOS Developer Library links: https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html
What I'd like to do is be able to access the individual components of the address field to make a string like this:
$address = get_post_meta($post_object->ID, 'location_address');
$address1 = $address[0]['address1'];
$address2 = $address[0]['address2'];
$city = $address[0]['city'];
$state = $address[0]['state'];
$postal_code = $address[0]['postal_code'];
echo '<a href="http://maps.apple.com/?q=' . the_title() . ', ' . $address1 . ', ' . $address2 . ', ' . $city . ', ' . $state . ' ' . $postal_code . '">' . the_field('location_address') . '</a>';
Does that make sense?
@industriousind What is the name of your address field? Does it match "location_address" or did you name it something else? Also, double check to ensure the $post_object->ID is actually returning the ID of the item that you are trying to retrieve the data for. If you are calling that code outside of the loop on the page, it may not have a value for the $post_object->ID variable which would cause it to be empty and fail.
@indianabenny yes, "location_address" is correct, $post_object->ID may be the problem, I'll mess with that. I'm using it on pages, and it's above the "the_content()" tag. I'm not having any issues with the other ACF "get_field()" tags, so I assumed $post_object->ID was ok.
@industriousind try changing $post_object->ID to $post->ID and see if that works.Or just set a variable that has the page/post ID and pass that in place of the $post_object->ID
@indianabenny that was it! Thank you!
@industriousind Glad that worked. Cheers.
Can you provide some documentation for retrieving specific fields from the address group? For example, I'd like to be able to print out the City field separate from the rest. Thanks!