Return all custom meta data for one custom post type

I’ve set up a custom post type which has three custom meta fields: name, latitude, longitude. Each post already shows the name on an integrated map based on it’s latitude and longitude.

I now would like to add a page to my site which shows ALL the names on a map based on their latitude and longitude.

I obviously know how to get single values out and get them on a map, but I’m not that experienced in WordPress so I’m not sure what the best way would be to get all this information out in a structured way for what I’m trying to do. Any pointers to get me started would be appreciated.

5 s
5

If all of your custom post type posts have all meta fields that you need then you can use the fields argument and set it to ids which will work much faster for example:

//get your custom posts ids as an array
$posts = get_posts(array(
    'post_type'   => 'your_post_type',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'fields' => 'ids'
    )
);
//loop over each post
foreach($posts as $p){
    //get the meta you need form each post
    $long = get_post_meta($p,"longitude-key",true);
    $lati = get_post_meta($p,"latitude-key",true);
    $name = get_post_meta($p,"name-key",true);
    //do whatever you want with it
}

Leave a Comment