Display all post meta keys and meta values of the same post ID in wordpress

I’m trying to display post meta values and post meta keys, If only one value is to be display I can used the simple function get_post_meta() but what I need now is to post all post meta data with the same post_id. I tried using foreach loop but nothing displays. Can you please check my code?

function wpt_calendar_display()
{
    global $post;

    $columns = array(
        'date_event' => 'Date',
        'name_event' => 'Event'
    );
    register_column_headers('list-header_events', $columns);

    $event_name = get_post_meta( $post->ID, '_event_name' );
   // $event_date = get_post_meta( $post->ID, '_event_date', false );

    $return .= "<table class="widefat">";
    $return .= "<tr>";
    $return .= print_column_headers('list-header_events');
    $return .= "</tr>";
    $return .= "<tr>";

    if (!empty($event_name))
    foreach($event_name as $e_name)
    {
        $return .= "<td>";
        $return .= $e_name;
        $return .="</td>";
      
    }

    $return .= "<td>";
    
    $return .= "</td>";
    $return .= "</tr>";
    $return .= "</table>";
    return $return;
}

Related posts

Leave a Reply

6 comments

  1. Default Usage

    Get the meta for all keys:

    <?php $meta = get_post_meta($post_id); ?>
    

    Get the meta for a single key:

    <?php $key_1_values = get_post_meta( 76, 'key_1' ); ?>
    

    for example:

    $myvals = get_post_meta($post_id);
    
    foreach($myvals as $key=>$val)
    {
        echo $key . ' : ' . $val[0] . '<br/>';
    }
    

    Note: some unwanted meta keys starting with “underscore(_)” will also come, so you will need to filter them out.

    For reference: See Codex

  2. $myvals = get_post_meta( get_the_ID());
    foreach($myvals as $key=>$val){
      foreach($val as $vals){
        if ($key=='Youtube'){
           echo $vals 
        }
       }
     }
    

    Key = Youtube videos all meta keys for youtube videos and value

  3. As of Jan 2020 and WordPress v5.3.2, I confirm the following works fine.

    It will include the field keys with their equivalent underscore keys as well, but I guess if you properly “enum” your keys in your code, that should be no problem:

    $meta_values   = get_post_meta( get_the_ID() );
    $example_field = meta_values['example_field_key'][0];
    
    //OR if you do enum style 
    //(emulation of a class with a list of *const* as enum does not exist in PHP per se)
    $example_field = meta_values[PostTypeEnum::FIELD_EXAMPLE_KEY][0]; 
    

    As the print_r(meta_values); gives:

    Array
    (
        [_edit_lock] => Array
            (
                [0] => 1579542560:1
            )
    
        [_edit_last] => Array
            (
                [0] => 1
            )
    
        [example_field] => Array
            (
                [0] => 13
            )
    )
    
    

    Hope that helps someone, go make a ruckus!

  4. I use it in form of a meta box. Here is a function that dumps values of all the meta data for post.

        function dump_all_meta(){
    
            echo "<h3>All Post Meta</h3>";
    
            // Get all the data.
            $getPostCustom=get_post_custom();
    
    
            foreach( $getPostCustom as $name=>$value ) {
    
                echo "<strong>".$name."</strong>"."  =>  ";
    
                foreach($getPostCustom as $name=>$value) {
    
            echo "<strong>".$name."</strong>"."  =>  ";
    
            foreach($value as $nameAr=>$valueAr) {
                    echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    echo $nameAr."  =>  ";
                    echo var_dump($valueAr);
            }
    
            echo "<br /><br />";
    
            }
        } // Callback funtion ended.
    

    Hope it helps. You can use it inside a meta box or at the front-end.