So I'm at my wit's end here. I've done this before plenty of times, but I think I have stared at this so much I am missing something.

  • I basically have a custom post called locations. That custom post has a few fields which include address, and latitude and longitude.

    • that lat/long are geocoded so they get automatically populated after the user enters in the address (so they can later be dumped into a map):

    the_field('latitude'); ?> the_name(); ?>" value="the_value(); ?>"/>

    the_field('longitude'); ?> the_name(); ?>" value="the_value(); ?>"/>

  • The second part of this is the people custom field that has a select box that lets the user select the title of each of those locations:

    the_name(); ?>"> Select Location ID; ?>" the_select_state($office->ID)?>>post_title; ?>

The final piece that I am having issues with is tying them together. I have a query displaying the information for the people:

Based on the region selection in the people custom post, I want to display the last and long fields in the locations post. I've done something similar before without any issues, but this time around it's not cooperating. I've tried different variations, but here is where I am right now:

        $people = get_posts(array(
        'post_type' => 'person',
        'nopaging' => 'true',
        'order' => 'ASC'

    foreach ($people as $person) :
        $office = 'office';
        $office_id = get_post_meta($person->ID, 'person_location', TRUE);

   <?php get_post_meta($office_id, "latitude", true); ?>', '<?php echo get_the_title($office_id); ?>

I've tried different things including creating a custom query with no avail:

function get_latLong($office_id) {
    global $wpdb;

    $mylink = $wpdb->get_row("SELECT * FROM $wpdb->location WHERE id ='" . $office_id . "'");
    echo $mylink->latitude;


Any insight would be greatly appreciated! Thanks

  • So just to be clear, you want the user to pick a location in a drop down, then pick a person at that location in a second drop down, but the second drop down must only contain people in that location and not a full list of people?
    – Tom J Nowell
    Feb 3, 2012 at 17:52
  • So there are two custom posts. The one for locations where you enter the location name, address, and the lat/long are geocoded automatically into invisible input boxes. The custom posts for people has a select box that's pulling the title/value(id in this case) of the locations. When running a loop for the people custom post in my template I'd like to spit out the latitude and longitude input boxes based on the select option selected in the people post.
    – tedi
    Feb 3, 2012 at 18:00
  • You're questions written a little confusingly.
    – Tom J Nowell
    Feb 3, 2012 at 18:01
  • So you have a person post type, and an office/location post type. Each person has an office, and the ID of the office is stored in meta, right?
    – Tom J Nowell
    Feb 3, 2012 at 18:02
  • You want to get the longitude and latitude of the office a person resides at?
    – Tom J Nowell
    Feb 3, 2012 at 18:02

2 Answers 2


Could it be that:

<?php get_post_meta($office_id, "latitude", true); ?>

Should be

<?php echo get_post_meta($office_id, "latitude", true); ?>

AKA a missing echo statement?

  • Tried that without much luck.
    – tedi
    Feb 3, 2012 at 18:05

Given the post types person and location, this will print out the latitude and office of every person:

$q = new WP_query(array( 'post_type'=> 'person'));
        $location_id = get_post_meta($post->ID, 'person_location', true);
            echo '<p>'.get_the_title($location_id).' is at latitude: "'.get_post_meta($location_id, "latitude", true).'"';
        } else {
            // something went wrong here

Put an error debug call in the else statement, and if the latitude isn't set you'll get a latitude of "", which should indicate where to bughunt

  • Thanks Tom! However I'm afraid that doesn't work. That's part 1 where it displays the selection from the person custom post. The next step which is where I'm stuck in, is to compare that to the locations custom post, to spit out the latitude and longitude of that location based on the person post selection.
    – tedi
    Feb 3, 2012 at 18:22
  • Got it. So this works: $office_id = get_post_meta($leader->ID, 'leader_location', TRUE); ?> ['<?php echo get_post_meta($office_id, "latitude", true); ?>,<?php echo get_post_meta($office_id, "longitude", true); ?>
    – tedi
    Feb 3, 2012 at 18:31
  • It would appear you're missing vital information about your UI design and how you want it to work. E.g. do you have a combobox of people that gets submitted as a form? Are you expecting it to do it all on the one page via javascript?
    – Tom J Nowell
    Feb 4, 2012 at 15:09
  • Or are you wanting to show it on an individual person post once clicked through?
    – Tom J Nowell
    Feb 4, 2012 at 15:10
  • What I'm saying is you've described your solution and that it's broken without properly defining in solid terms the original problem
    – Tom J Nowell
    Feb 4, 2012 at 15:10

Your Answer

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

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