skip to Main Content

This is what I’m doing:

$content = array(get_post_meta($postId, 'content'));
$media = array(get_post_meta($postId, 'media'));
$yt = array(get_post_meta($postId, 'youtube'));
$max = max(count($content), count($media), count($yt));
$combined = [];
for($i = 0; $i <= $max; $i++) {
    if(isset($content[$i])) {
        $combined[] = ["type" => "content", "value" => $content[$i]];
    }
    if(isset($media[$i])) {
        $combined[] = ["type" => "media", "value" => $media[$i]];
    }
    if(isset($yt[$i])) {
        $combined[] = ["type" => "youtube", "value" => $yt[$i]];
    }
}
foreach ($combined as $key => $val) {
    echo '<li>'.$val['value'].'</li>';
}

The result is:

Array
Array
Array

I’d expect:

media value
content value
youtube value

2

Answers


  1. You are working with array of arrays because get_post_meta returns by default an array, unless the 3rd parameter $single is true.

    So try

    $content = array(get_post_meta($postId, 'content', true));
    

    or actually just

    $content = get_post_meta($postId, 'content');
    
    Login or Signup to reply.
  2. You realise that

    $content = array(get_post_meta($postId, 'content'));
    

    creates $content as an array with a single value? (the value is whatever is returned by your get_post_meta() function)
    (and similarly for $media and $yt)
    Each of these three will always have one and only one element.

    Looking at the code that follows this, I’m not sure if this is what you intended. $max will always be one, for example, and $content[$i] will always be the value returned by your get_post_meta() function – based on the result shown, presumably an array

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search