2

This might might be more of a PHP question - but does deal with a WordPress plugin, Advanced Custom Fields

I am using a global select option to store a specific Location (custom post type) as the headquarters. I am storing the value as such:

function options_headquarters() {
    foreach(get_field('option_headquarters','options') as $post_object) {
        $options_headquarters = $post_object->ID;
        return $options_headquarters;
    }
}

What I am fuzzy on is - since my option is only allowed to grab one value -- is there an alternative to using a foreach statement (to grab just a specific array value) and still applying it to the post_object?

That function stores the postID of that Location. I am currently outputting this in my template as:

echo get_field('location_phone',options_headquarters());

which grabs a field called location_phone (a meta value within the Location custom post type) and specify a specific postID to grab the value from (the headquarters). Is there a better way of handling passing in this headquarters value? Just pulling this out of you know where, but something like options_headquarters('location_phone'); would be pretty sweet ;)

Thanks!

3
  • function options_headquarters() { $headquarters_field = get_field('option_headquarters','options'); $headquarters = isset($headquarters_field[0]) ? $headquarters_field[0]->ID : NULL; return $headquarters; } Did it for me :)
    – Zach
    Dec 19, 2011 at 19:42
  • Zach, please add the solution as an Answer and mark it solved, so it gets out of the Unanswered category...
    – brasofilo
    Jul 18, 2012 at 20:35
  • Cool! One last thing: in the Q, when you say "[...]using a global select option to store a specific CPT [...], do you mean "...using a Post Object Field Type..."? . . . Snapshot.
    – brasofilo
    Jul 18, 2012 at 21:12

1 Answer 1

1

This ended up working for me:

function options_headquarters() { 
    $headquarters_field = get_field('option_headquarters','options'); 
    $headquarters = isset($headquarters_field[0]) ? $headquarters_field[0]->ID : NULL; 
    return $headquarters; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.