Home › Forums › General Issues › Accessing ACF author field in a function
I have a custom field ‘zipcode’ and this is setup as a custom Author field. I’m using Relevanssi to do a standard search with the Authors as opposed to blog posts or pages. On the results page, I’m able to, in the loop, retrieve the custom zipcode field as such:
$zipCode = get_field('nestwell_user_b_zipcode', 'user_'.$userID );
and then I’m free to use the $zipCode variable to pull in each users zip code. However… I have a function in functions.php that needs to access this zipcode. For the life of me i can’t seem to figure it out. I need to construct a custom URL with the users zipcode from ACF in this function. Right now the $to variable is hard coded to a zipcode and it all works great. Though I need it to be dynamic for each user in the loop.
How can I achieve this??
function google_distance_between_places( $from, $to ,$decimals = 0 ){
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
parse_str(parse_url($url)['query'], $zipURL);
$from = $zipURL['zip'];
$to = '99141';
$data = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$from&destinations=$to&key=xxx&language=en-EN&sensor=false");
$data = json_decode($data);
$distance = 0;
if( !empty( $data->rows[0]->elements ) ){
foreach($data->rows[0]->elements as $road) {
if( $road->status == 'OK' ){
if( isset( $road->distance->value ) ){
$distance += $road->distance->value;
}
$convert_miles = 1609.344;
$distance = ( $distance / $convert_miles );
$distance = round( $distance,$decimals );
}else{
$distance = "not_found";
}
}
}
return $distance;
}
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.