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.


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
  • Bainternet, with some tweaks to your code I've been able to get at least all the information on my page. Still having some minor issues, but it looks like I'll be able to get this working now. – WouterB Mar 12 '12 at 14:21
  • Glad i could help – Bainternet Mar 12 '12 at 14:33
  • Maybe a quick follow up question. Instead of get_post_meta, I used $name = get_the_term_list($p, "name-key",true); to get one of my values. This keeps however adding a "1" in front of the name. No idea where it comes from. – WouterB Mar 12 '12 at 15:35
  • 1
    That's because get_the_term_list is used to get a term list of a taxonomy and not the meta of a post. – Bainternet Mar 12 '12 at 16:05
  • Yes, I needed a single term of a taxonomy. It returns the value, but also a "1" and it does so in every instance returned by the foreach loop. – WouterB Mar 12 '12 at 16:34

This code will give you all the posts with a longitude, latitude, and name assigned. Then you can loop through them to do your output and such.

$args = array(
    // basics
    'post_type'   => 'your_post_type',
    'post_status' => 'publish',

    // meta query
    'meta_query' => array(
        'relation' => 'AND',
            'key'     => 'longitude-key',
            'value'   => '',
            'compare' => 'NOT'
            'key'     => 'latitude-key',
            'value'   => '',
            'compare' => 'NOT'
            'key'     => 'name-key',
            'value'   => '',
            'compare' => 'NOT'
$posts = new WP_Query( $args );

Naturally you're gonna needa modify that, and I strongly recommend reading the WP_Query documentation to get that tuned exactly how you want, but that'll do it for you. If you use that on a template, the whole thing should be a piece of cake.

  • Thanks, when printing the returned array, that didn't exactly return what I was expecting. I guess I'll have to look in to that again. – WouterB Mar 11 '12 at 17:49
  • That should give you an array of post objects in the set post type where longitude-key, latitude-key, and name-key are not blank. Does it not do this? – mor7ifer Mar 11 '12 at 18:01
  • For now I took the name-key out and tested the code. I've printed the returned array, and copy pasted the result over to pastebin to let you see what it returns. http://pastebin.com/uhQHSths – WouterB Mar 12 '12 at 11:11
  • Looks like the query isn't executing, try declaring the WP_Query object separately then executing the query() method with $args. – mor7ifer Mar 12 '12 at 21:42

Why not use get_metadata()?

If you pass the properly parameters you get all the meta attributes of a post (custom or not).


$META_ATTRIBUTES = get_metadata( 'post', get_the_ID(), '', true );

Note that I didn't use my cpt machine name as first parameter; you have to use 'post' or the function will return nothing.

Setting the third parameter (meta key) to an empty string you are telling to the function to retrieve all the meta attributes of the post.


This is the best way to get meta values in Custom post types include this code in function.php it include metaval array to response including all meta values

add_action( 'rest_api_init', 'create_api_posts_meta_field' );

function create_api_posts_meta_field() {

 // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
 register_rest_field( 'tour', 'metaval', array(
 'get_callback' => 'get_post_meta_for_api',
 'schema' => null,

function get_post_meta_for_api( $object ) {
 //get the id of the post object array
 $post_id = $object['id'];

 //return the post meta
 return get_post_meta( $post_id );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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