Forums

the_meta WORKS, get_post_meta does not (5 posts)

  1. gvtexas
    Member
    Posted 1 month 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. apljdi
    Member
    Posted 1 month ago #

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

  3. gvtexas
    Member
    Posted 1 month ago #

    No, get nothing.

  4. esmi
    Member
    Posted 1 month ago #

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

  5. gvtexas
    Member
    Posted 1 month ago #

    Thanks for the direction tip. This code:

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

    ...finally did the trick.

Reply

You must log in to post.

About this Topic