Home / functions.php / Get all post meta data
  • How to Get all post meta data

    no thumbnail sorry: Get all post meta data

    Adding this PHP code to your wordpress functions.php will return all meta data for a single post.

    ( click code to copy )

    wordpress snippet : PHP

    function get_post_meta_all($post_id){
        global $wpdb;
        $data   =   array();
            SELECT `meta_key`, `meta_value`
            FROM $wpdb->postmeta
            WHERE `post_id` = $post_id
        foreach($wpdb->last_result as $k => $v){
            $data[$v->meta_key] =   $v->meta_value;
        return $data;

    ( WordPress codex functions, hooks, in this snippet. )

    query, get_post, get_post_meta, wp,
  • thansk so much !

    • No problem glad you found something that helps out.

  • Jract7

    Kevin, thank you for all these great snipps… very helpful…I’m new to WP and I am interested in expanding on this and the track and display posts snipps.What I’m looking for is a way to track who and when a JSA is viewed…I want to place a button/link on the bottom of a post that, when clicked, will grab the user’s name, date and the title of that post and place them in a table so i can view them or export them.I have no clue how to go about this and I’ve tried a bunch of plugins with no avail. So i jumped to the code side and wpdb is not working like I think it should. Could you point me in the right direction on how to get this done?

    • Hi Jract7,

           For something this specific you should pay a developer to create this for you. This is not something you want to try and save to post meta. One developer I would suggest is Michael at http://www.problogdesign.com/services/ you can see his services listed here. Let him know Kevin from wpsnipp.com sent you and see if something like this is within your budget. He has a form you can fill out on the right hand side of the site.

  • Jepser

    You can use get_custom_meta() instead. It’s better and it’s a built in wordpress function.

    • Hi Jepser, very true. This post was made in 2010 Ill amend it with an updated snippet. Cheers.

  • Anand

    $meta_values = get_post_meta($post_id);

    > If only $id is set it will return all meta values in an associative array.

  • Bruno

    thank you! greetings from Brazil!!