I have a custom post type called vacancies and another called our_homes

How do I get the google map coordinates from our_homes and display inside the vacancies single post template?

My attempt below shows my tragic attempt at code inside the single-vacancies.php file:

    //Query custom post type our_homes and display tabs for each
    $query = new WP_Query( array( 'post_type' => 'our_homes', 'field' => 'slug', 'posts_per_page' => 999 ) ); 

    if ( $query->have_posts() ) : 

    //$count = 1;
    //$title = the_title();
    //$location = get_field('google_map_coordinates');

    <?php while ( $query->have_posts() ) : $query->the_post(); 
        $location = get_field('google_map_coordinates', post_title);
    <?php if($title = $label) { echo $title; }

        //echo $label;


    <?php endwhile; wp_reset_postdata(); ?>

<?php endif; ?>
  • The output currently displays every post title of the custom post 'our_homes', with the matching post being repeated once in between every other post title.
    – Amesey
    Commented Jul 3, 2017 at 16:14
  • Where is the code that your using to get the custom field? and what is the name of yout custom field? Commented Jul 3, 2017 at 16:42
  • $location = get_field('google_map_coordinates');
    – Amesey
    Commented Jul 3, 2017 at 16:49

2 Answers 2


Although your question is confusing, but based on your attempts, I can say that you are using a loop inside another loop. You should store an array of main loop's titles in an array, and then write another loop outside the original loop and check the array.

So, this is what your main query would look like (I summarized it and removed the actual loop):

    // Define an empty array
    $posts_title = array();
        // Store each title inside the array
        $posts_title[] = get_the_title();

Now, you have an array of post titles. Write this query after the main query is finished, and closed:

$query = new WP_Query( 
        'post_type' => 'our_homes',
        'posts_per_page' => -1 
if ( $query->have_posts() ) { 
$count = 0;
    while ( $query->have_posts() ) { 
        // Check if this post's title matches the $label
        if( in_array( get_the_title(), $posts_title )) {
            // Do whatever you want

Also, if you need to get the post's or page's ID by their title, you can use get_page_by_title('TITLE HERE') function.


Try to do this inside the loop

$location = get_field('google_map_coordinates', get_the_ID);

See reference at ACF Documentation

  • I need to get the field of a post by post title. I have var called $label which also matches the post titles in the other post type. if the post type is equal to the label then display the coordinates of the post.
    – Amesey
    Commented Jul 3, 2017 at 17:04
  • The problem is that get_field only accepts the $post_id. You can try to check like this: if( $label == the_title() ) { $location = get_field('google_map_coordinates', get_the_ID); } Commented Jul 3, 2017 at 17:10
  • So it cant be done? i've managed to get the loop to display the post title, only problem is that it display it multiple times, infact the same number of times as there are posts. i will update my code above.
    – Amesey
    Commented Jul 3, 2017 at 17:13

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.