Forums

[resolved] get_post_meta - why won't you work :( (12 posts)

  1. invert
    Member
    Posted 3 years ago #

    Hi all, can anyone help? I'm trying to use the custom post meta in different (read:dirty evil hack) ways.

    Using the code below, I always get a T_IS_EQUAL error, if i change it to: != null, I get T_IS_NOT_EQUAL.

    The code is deffinately in the WP Loop. I need the code to work even if the Custom Meta Variable isn't set. (Hence the == Null)

    <?php
    if (get_post_meta($post->ID, keyValue, true)) == null {
    *** do this code ***
    } else {
    $imageName = get_post_meta($post->ID, keyValue, true);
    *** use above variable in this code ***
    } ?>

  2. moshu
    Member
    Posted 3 years ago #

    1. Don't yell in the title
    2. Total misunderstanding: you put the code itself in backtikcs, not the WORD code - you don't even need that word there :)

  3. invert
    Member
    Posted 3 years ago #

    Sorry moshu, my apologies, I've been beating my screen with my head for.. about an hour :( haha, and thanks for the tip on the code.

  4. Otto42
    Moderator
    Posted 3 years ago #

    get_post_meta($post->ID, keyValue, true);

    Are you using that code literally? Perhaps you should use this:
    get_post_meta($post->ID, 'keyValue', true);

    The key is a string. It has to be in quotes.

    Also, check your parenths.
    Also, you shouldn't compare it to null.

    That if statement should be:
    if (get_post_meta($post->ID, 'keyValue', true) == '') {
    *** do this code ***
    ...

  5. invert
    Member
    Posted 3 years ago #

    Hi Otto, thanks for your input,

    I put the **''** around the Key value, and changed Null to '', but it still results in the same Unexpected T_IS_EQUAL error.

    Any other ideas? :)

  6. Otto42
    Moderator
    Posted 3 years ago #

    invert: Again, double check your parenths. Your code above is wrong and has the parenths in the wrong places. Examine my code and examine your code and see the difference there.

  7. c00l2sv
    Member
    Posted 2 years ago #

    Hello!
    Thats strange, but is there somebody who tried the code above, because for me that code is not working.
    My code:
    <?php $key="mykey"; get_post_meta($post->ID, $key, true); ?>
    where 'mykey' is the custom field I defined when posting my article in CP.
    Need some help. Why it is not working?

  8. c00l2sv
    Member
    Posted 2 years ago #

    The only function that works for me is the_meta()!

  9. Otto42
    Moderator
    Posted 2 years ago #

    Your code doesn't work because you're not doing anything with the results from get_post_meta(). You get it, but then don't display it or put it in a variable or anything else.

    This will output the resulting meta value (notice the addition of "echo"):
    <?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>

    Or maybe you want to use it for something else?

    <?php $key="mykey"; $value =get_post_meta($post->ID, $key, true);
    echo "Value: ".$value;
    ?>

    That's how you should do it.

  10. c00l2sv
    Member
    Posted 2 years ago #

    Ooh, thanks!
    I was thinking the function is doing that for me, like the_meta()
    Maybe we should update the wiki?

  11. joshkaufman
    Member
    Posted 2 years ago #

    I was having the exact same problem as cool2sv using the get_post_meta function. To me it was not clear in the codex that you had to echo the result of the function. I assumed it would echo itself in a similar fashion to the_meta();

    Thanks for the helpful resolution Otto42!

    Josh from
    http://www.video-tabs.com

  12. c00l2sv
    Member
    Posted 2 years ago #

    Done, finally updated the codex!

Topic Closed

This topic has been closed to new replies.

About this Topic