I'm using a plugin that allows me to add people profiles as a custom post type, with several custom fields used for specific information. These posts are then output via shortcode on the relevant page, with the plugin taking care of outputting the relevant data.

Additionally Ive created a new page template for the page on which this info will be output. I need to output -from this page template- all the 'locations' entered in the relevant field when creating each person profile.

I know this info is stored in wp_postmeta, but haven't been able to work out if WP offers a method to do this natively / or how to do this.

I've so far got the following code:

//get your custom posts ids as an array
                $posts = get_posts(array(
                    'post_type'   => 'team-member',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'fields' => 'ids'
                //loop over each post
                foreach($posts as $p){
                    //get the meta you need from each post
                    $locations = get_post_meta($p,"_location",true);

But, this only appears to show the location field for the first profile shown.

Any ideas where I'm going wrong &/or what I need to actually do to get the recorded locations? Ideally as a unique list(eg no repeated locations).

1 Answer 1


Your call should include the ID:


And what is $locations ?
If an array, it should be $locations[] = If a string, it should be $locations .=

If you only want unique locations, you could do this:

$locations = array();
foreach($posts as $p){
    $location = get_post_meta($p->ID,"_location",true);
    if( ! in_array( $location, $locations) )
        $locations[] = $location;
  • Thanks for the reply. The problem was simply that the '.' was missing from '$locations .='. Clearly a 'woods for the trees' moment on my part. Jan 9, 2015 at 9:43

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.