0

I have a custom field that contains a list of comma separated colors.

I want to randomly select one of those colors.

If get_post_meta returns an array I thought I could randomly pick from that array.

If I try to echo $rand_color_value it just gives me the whole array

How can I randomly select one color from the list in the custom field.

    $agrs = array(
        'post_type' => 'page',
        'tag' => 'colors'
    );

    $colorLoop = new WP_Query($agrs);

    if($colorLoop->have_posts()):
        while($colorLoop->have_posts()):
            $colorLoop->the_post();

    $theColor = get_post_meta($post->ID, 'colors', false);

    /*random color*/
    $rand_color = array_rand($theColor,1);
    $rand_color_value = $theColor[$rand_color];
    echo $rand_color_value; 

    ?>

    <?php endwhile; endif;?>

    <?php wp_reset_postdata; ?>
4
  • What do you mean by "a list of comma separated colors"? A string of comma separated values? Or do you actually store an array in the option? Also what exactly do the variables $theColor, and $theWeight contain? Try var_dump(compact('theColor', 'theWeight')); – ndm Jun 12 '13 at 14:36
  • The custom field contains something like: red,green,blue,brown. $theColor should contain the array from the custom field. $theWeight was a typo i have updated it now. From the custom field i want to pick a random colour like red. – Simon Cooper Jun 12 '13 at 14:43
  • I'm still not sure about the actual data type stored in your meta data, your showing a string but you're talking about the option containing an array? If it's a string then I can tell you what the problem probably is, if it's an array then you'll need to do some additional debugging. – ndm Jun 12 '13 at 14:55
  • It's a string, in the custom field called 'colors' I type: red,blue,green,brown. I want to randomly pick one of these colours and display it. I thought get_post_meta would return an array showing each color at a separate index in the array – Simon Cooper Jun 12 '13 at 15:03
1

As the content of the meta data is actually a string (according to your comment), your code won't work. get_post_meta(), respectively get_metadata() will only deserialize the content if it's actually serialized.

So, if you can't change how the data is saved, ie make sure that an actual array or already serialized content is passed to update_post_meta(), then you have to manually explode your data into an array:

$theColor = get_post_meta($post->ID, 'colors', true);
$theColor = explode(',', $theColor);
2
  • I thought explode would work but it doesn't seem to. Is it because this is a string at [0] in an array? – Simon Cooper Jun 12 '13 at 16:00
  • Yep, sorry, I forgot to change the $single parameter in the get_post_meta() call. I've updated the code in my answer. – ndm Jun 12 '13 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.