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.

Read More

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.

Related posts

Leave a Reply

5 comments

  1. 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
    }
    
  2. 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.

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

  4. 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 );
    }
    
  5. 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.

    $args = array(  
        'post_status' => 'publish',
        'posts_per_page' => 5,  
        
        );
    
    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
        the_title(); 
    the_excerpt();
    endwhile;