user1950349
user1950349

Reputation: 5146

$post->post_meta not pulling any post information in wordpress/php

I have a function in php as shown below:

function hello_world( $post ) {
    echo '<pre>'; print_r($post); echo '</pre>'; // Line A
    echo '<pre>'; print_r($post->post_meta); echo '</pre>'; // Line B
    return $post->post_meta;
}

I have added Line A and Line B in the function above for debugging purposes. In the function hello_world above, Line A returns list of all posts whereeas Line B doesn't return anything.

I am wondering what changes I need to make in the wordpress or php code above so that Line B returns list of posts.

Upvotes: 0

Views: 53

Answers (1)

FluffyKitten
FluffyKitten

Reputation: 14312

Post_meta doesn’t contain a list of posts, it has extra information that you, a plugin or your theme are adding to a post using add_post_meta.

To get the post meta, you use the get_post_meta function.

To get all values you can do this:

$allpostmeta = get_post_meta ($post->ID);

You can also get a specific field like this:

$mymetavalue = get_post_meta ($post->ID, ‘your_meta_key’);

Upvotes: 1

Related Questions