GCX / acf-address-field

Advanced Custom Fields - Address Field add-on
BSD 3-Clause "New" or "Revised" License
13 stars 13 forks source link

Retrieve individual fields from the group #5

Open kmmathis opened 12 years ago

kmmathis commented 12 years ago

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!

TheJester12 commented 12 years ago

Yes, this would be helpful.

ginolomelino commented 11 years ago

Same here. Any way to do this?

indianabenny commented 11 years ago

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'];
}
grimmwerks commented 11 years ago

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)?

justintucker commented 11 years ago

@indianabenny Thanks man. This is exactly what I needed.

indianabenny commented 11 years ago

@justintucker Glad it was helpful.

farnsco commented 11 years ago

@indianabenny is your code still valid? I've been trying it and it's not rendering anything.

indianabenny commented 11 years ago

@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?

indianabenny commented 11 years ago

FYI, if you need a post 4.0 version of this plugin, you can find one here: https://github.com/strickdj/acf-field-address

farnsco commented 11 years ago

@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?

indianabenny commented 11 years ago

@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.

farnsco commented 11 years ago

@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.

indianabenny commented 11 years ago

@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

farnsco commented 11 years ago

@indianabenny that was it! Thank you!

indianabenny commented 11 years ago

@industriousind Glad that worked. Cheers.