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 toids
which will work much faster for example: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.
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.Why not use get_metadata()?
If you pass the properly parameters you get all the meta attributes of a post (custom or not).
Example:
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
Here we showing an example to relative displaying custom post type in WordPress and in this code have args in arrays and using wp_query with the loop for getting post data.