the_meta WORKS, get_post_meta does not
-
Extensive searching and tweaking failed to make this work. I’m trying to pull one custom field value into a post (the same post that has the custom field).
CANNOT get any variant of get_post_meta to work. If I use the_meta(), the field shows, but so do all the fields as expected.
PHP is executable in my post via a plugin (and I have other PHP statements working fine in the test post).
I’ve probably tried twenty ways to do this, include these approaches (not all together, one at a time):
<?php $key="cost"; echo get_post_meta($post->ID, $key, true); ?> <?php $key="cost"; print get_post_meta($post->ID, $key, true); ?> <?php $cost = get_post_meta($post->ID, 'price', true); ?> <?php print($cost) ?> <?php $cost = get_post_meta($post->ID, 'price', false); ?> <?php echo ($cost) ?>
…and every combination of true/false, print/echo, $value = true, true, etc. etc.
I’ve seen a few posts where folks running the same version (2.8.4) are claiming get_post_meta quit working when they upgraded. Is there an issue with this version, or have I just not found the magic version #21? 😉
Thx.,
Gary
-
If you
echo $post->ID;
do you get a valid value?No, get nothing.
Then that’s your problem. get_post_meta() can’t do anything without a valid id.
Thanks for the direction tip. This code:
<?php echo get_post_meta(get_the_ID(), "Price", true); ?>
…finally did the trick.
Dope… many thanks for this.
Mental note:
get_the_ID is a function which retrieves the ID of the current item in the WordPress Loop, providing access to meta data for each post in the loops array.nice work:-D
I have this problem but in my code I have the ID, it just doesn’t work for some fields. I use Magic Fields plugin for attaching custom fields to posts.
It prints the number type fields, but not the text ones. Did any of you got this bug?
Thanks!
- The topic ‘the_meta WORKS, get_post_meta does not’ is closed to new replies.