How can I create a list of page titles from custom meta values?

I want to associate pages with other pages by using custom meta values.

For one page, I want to associate two other pages, so I create a custom meta value with the key Procedure and the value of the other page ID, one for each page.

Then I want to display these associated pages, as their page titles, but in a comma separated list:

<?php echo implode(', ',get_post_meta($patient_story->ID, 'Procedure', false)); ?>

This is nearly there, but it only displays the page IDs (as it’s giving the direct values in from the custom meta entries). I can’t seem to integrate get_the_title() without breaking the code.

What’s the best way I can have it so that it will convert:

Meta Key           Meta Value
Procedure    =>    238
Procedure    =>    240

into ‘Page title 1, Page title 2’

Solutions Collecting From Web of "How can I create a list of page titles from custom meta values?"

You need to get the title for each post ID in your array. The straightforward way is this:

$procedure_title_list = array();
$procedure_list = get_post_meta($patient_story->ID, 'Procedure', false);
foreach ( $procedure_list as $procedure ) {
    $procedure_title_list[] = get_the_title( $procedure );
}
echo implode( ', ', $procedure_title_list );