• Resolved invert

    (@invert)


    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 ***
    } ?>

Viewing 11 replies - 1 through 11 (of 11 total)
  • 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 ??

    Thread Starter invert

    (@invert)

    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.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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 ***
    ...

    Thread Starter invert

    (@invert)

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

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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.

    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?

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

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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.

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

    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
    https://www.video-tabs.com

    Done, finally updated the codex!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘get_post_meta – why won’t you work :(’ is closed to new replies.