Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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 codes?

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;
}
like image 453
user2901740 Avatar asked Mar 06 '14 05:03

user2901740


People also ask

How do I get all meta data in WordPress?

If you wanted to see all the post meta keys and values for a post,page or custom post type in WordPress you can either see them in the database in the wp_postmeta table or you could use the get_post_meta function to retrieve all the post meta or a specific key.

How do I get post meta value by post ID?

get_post_meta( int $post_id, string $key = '', bool $single = false ): mixed. Retrieves a post meta field for the given post ID.

What is WP Postmeta?

Post meta data is information about a post, such as the date and time the post was published and the post author. The default meta data displayed with each post depends on which WordPress theme the site is using but usually includes some combination of the date, author, and post categories or tags.


3 Answers

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

like image 132
Gopal S Rathore Avatar answered Oct 10 '22 21:10

Gopal S Rathore


To get all rows, don't specify the key. Try this:

$meta_values = get_post_meta( get_the_ID() );

var_dump( $meta_values );

Hope it helps!

like image 6
Deepak Arora Avatar answered Oct 10 '22 20:10

Deepak Arora


$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

like image 2
alpc Avatar answered Oct 10 '22 20:10

alpc