the_meta WORKS, get_post_meta does not (8 posts)

  1. gvtexas
    Posted 6 years ago #

    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? ;-)


  2. s_ha_dum
    Posted 6 years ago #

    If you echo $post->ID; do you get a valid value?

  3. gvtexas
    Posted 6 years ago #

    No, get nothing.

  4. esmi
    Forum Moderator
    Posted 6 years ago #

    Then that's your problem. get_post_meta() can't do anything without a valid id.

  5. gvtexas
    Posted 6 years ago #

    Thanks for the direction tip. This code:

    <?php echo get_post_meta(get_the_ID(), "Price", true); ?>

    ...finally did the trick.

  6. Joel
    Posted 5 years ago #

    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.

  7. eivindvr
    Posted 5 years ago #

    nice work:-D

  8. conualfy
    Posted 5 years ago #

    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?


Topic Closed

This topic has been closed to new replies.

About this Topic