wordpress snippets last updated   : October 6, 2014
we now have 641 snippets

Get all post meta data

wordpress snippet

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

snippet :  PHPcopy
function get_post_meta_all($post_id){
    global $wpdb;

    $data   =   array();

    $wpdb->query("
        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;
}

Did you like this post? If so please share it with your friends. Thanks!

  • http://muasamvui.com Muasamvui

    thansk so much !

    • http://wpsnipp.com Kevin Chard

      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?

    • http://wpsnipp.com Kevin Chard

      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.

    • http://wpsnipp.com Kevin Chard

      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!!