Home > Mobile >  WordPress get value from postmeta by post id
WordPress get value from postmeta by post id

Time:06-29

I have WordPress website and I want to get meta_value (7) from ej_postmeta table where post_id = 200 Also table postmeta has prefix ej_ and I want to show result in header.php.

Database structure

meta_id | post_id | meta_key | meta_value

100 | 200 | new_job_posted | 7

So I use get_post_meta function in header.php

I try ( header.php )

 global $wpdb;
    $wpdb->show_errors();
    $values = get_post_meta( 200, 'meta_value' );
    echo $values;

and


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

But I get nothing

CodePudding user response:

The second parameter should be the meta key name. https://developer.wordpress.org/reference/functions/get_post_meta/

This line

$values = get_post_meta( 200, 'meta_value' );

should be

$values = get_post_meta( 200, 'new_job_posted' );
  • Related