WordPress.org

Ready to get started?Download WordPress

Forums

the_meta WORKS, get_post_meta does not (8 posts)

  1. gvtexas
    Member
    Posted 4 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? ;-)

    Thx.,
    Gary

  2. s_ha_dum
    Member
    Posted 4 years ago #

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

  3. gvtexas
    Member
    Posted 4 years ago #

    No, get nothing.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

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

  5. gvtexas
    Member
    Posted 4 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
    Member
    Posted 4 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
    Member
    Posted 3 years ago #

    nice work:-D

  8. conualfy
    Member
    Posted 3 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?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic