Support » Plugins » Hacks » widget spitting out values as arrays instead of single items

Viewing 5 replies - 1 through 5 (of 5 total)
  • You need to set the third parameter of get_post_meta to true in order to return a single value, default is false (an array of values).


    $my_field = get_post_meta( $post->ID, 'your-field-name', true );
    if( $my_field )
       // Do whatever

    Thanks Mark

    Ooops didn’t know about pastebin
    Here’s the code

    Lines 80 and 81 are using the format you describe above and returning nothing due true referring to ‘is it a single item or an array?’

    For some reason it’s returning as an array, if I set TRUE then it spits out nothing. If I set FALSE then I still have the problem that it’s an array.

    What I need to know is WHY it’s making the results in my widget an array (with only 1 item) and how I can fix it.


    Not sure i follow quite what it is the widget is doing but i see a few problems in your code.. so i’ll highlight them so you can make corrections.

    Your calls to get_post_custom_values will always give you an array, that’s what that function is for, this will do pretty much the same as get_post_meta except there’s no option for returning a singular value (it’s always an array or nothing). Use get_post_meta as you have a few lines down if you want only a single value returned.

    First loop..

    foreach ( $link as $RNMkey => $RNMvalue ) {    $cvalue=$RNMvalue;   }

    $cvalue will only ever be the value of the last array item, since you’re assigning the same variable with each iteration of the foreach loop (the last result will be what’s stored in $cvalue). If this is intended behaviour, you could do this instead..

    // Last item in array
    $cvalue = end( $link );
    // or
    // If you wanted the first item in array
    $cvalue = current( $link );

    Moving on, inside the next foreach loop are various lines like so..

    $RNWidgetCheck= Music;
    $RNWidgetCheck= Video;

    Your values should have quotes around them..


    $RNWidgetCheck = 'Video';

    I’m surprised the lines above didn’t cause PHP to throw up a few errors.

    Could quite possibly be other problems, but those are the ones that stand out on first glance, so i hope that helps.. 😉

    Thanks Mark!

    You’re welcome.. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘widget spitting out values as arrays instead of single items’ is closed to new replies.