Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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
}
share|improve this answer
    
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',
        array(
            'key'     => 'longitude-key',
            'value'   => '',
            'compare' => 'NOT'
        ),
        array(
            'key'     => 'latitude-key',
            'value'   => '',
            'compare' => 'NOT'
        ),
        array(
            '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.

share|improve this answer
    
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? –  m0r7if3r 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. –  m0r7if3r 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).

Example:

$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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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