I'm trying to output an array into one table cell. Here is a truncated version of what I have now:

$prerequisites = get_post_meta($post->ID, 'opl_prerequisites', true);

$output .= '<td>';
              if(count($prerequisites) === 1){
                foreach( $prerequisites as &$prerequisite ):
                  $prerequisite_object = get_post( $prerequisite );
                  $output .= $prerequisite_object->post_title;
              } else {
              foreach( $prerequisites as &$prerequisite ):
                $prerequisite_object = get_post( $prerequisite );
                $output .= $prerequisite_object->post_title.', ';
  $output .= '</td>';

This returns basically what I want, although I know there are definitely some issues with how I am calling it. I just want the contents of the array to be returned in one cell separated by commas without a comma being at the very end. Currently this code puts a comma at the end if there are more than 1 items in the array. A couple of issues I'm trying to figure out:

  • In the first IF statement, I have a foreach statement because I can't seem to get it to display the data inside the array unless I call it this way, even though there is only one item in the array. Is there a shorter way to get the post_title out? When I call it like $output .= $prerequisites[0]; it only gives me the post ID instead of the post_title.

  • I have messed around with the implode(); php function but I can't seem to find its exact use with this situation. I feel like that would be the short way to do what I'm trying to do.

Any help would be greatly appreciated. Thanks in advance!

2 Answers 2


Alternatively, make an array of the titles and then use implode...

$prerequisites = get_post_meta($post->ID, 'opl_prerequisites', TRUE);
$titles = array();
foreach( $prerequisites as $prerequisite ){
  $titles[] = get_the_title( $prerequisite );

$output .= '<td>'.implode(",", $titles).'</td>';
  • 1
    Thanks! This is exactly what I was looking for. Although same as the previous comment, it only worked when the 3rd parameter was TRUE.
    – AHinson
    May 17, 2016 at 12:01
  • Okay, corrected it... :-)
    – ToniWidmo
    May 17, 2016 at 13:01

Passing true as the 3rd parameter to get_post_meta() says to return only one item. Pass false, or leave it empty, to get an array of matching meta items.

The foreach loop below should work, then remove the extra comma if present.

$prerequisites = get_post_meta($post->ID, 'opl_prerequisites', FALSE);

$td_output = '<td>';
foreach( $prerequisites as $prerequisite ){
  $td_output .= get_the_title( $prerequisite ) . ', ';
if( strlen($td_output) > 4 ){
  // remove trailing comma if any prerequisite titles added
  $td_output = substr( $td_output,  0, -2 ); 
$td_output .= '</td>';
$output .= $td_output;
  • Thanks! This is works well. The only issue now is that the top cell of that column has no "prerequisites" and therefore no array. This substr seems to delete that top cell entirely and moves the next cell over into that spot. Any ideas?
    – AHinson
    May 17, 2016 at 3:10
  • The idea of the if(strlen()) was to be sure nothing was added to the string; obviously, there is other content in $output (my oversight). There are a couple of ways to deal with that, I'll edit my answer for one approach.
    – BillK
    May 17, 2016 at 3:15
  • Also, it only worked if the get_post_meta 3rd parameter was true. False only showed one returned result in the top row and nothing else below it. But the edit you made worked. Thanks a lot!
    – AHinson
    May 17, 2016 at 3:32
  • You're correct, I had the logic reversed. I hope that didn't cause you too much trouble.
    – BillK
    May 17, 2016 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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