I’ve created a custom post type and a corresponding meta box for a site I’m developing. As there are several entries for similar 'products', some of the post metas repeat, albeit with a trailing letter to distinguish them one from the other.

I’d like to know if there’s a way to call a post type by name while also incorporating a variable the end of the post_meta name so that it’s no longer necessary to list them all, but one to cover my bases.

Here’s what I refer to: trmet_tour_status_a, trmet_tour_status_b, trmet_tour_status_c and so forth are all custom fields which I need to display if they exist (there are at least seven of them). Is there a way I can use jQuery or a PHP function to change the trailing letter so I only need to write the code once?

<ul id="dateList">
<?php $dates_a = get_post_meta($post->ID, 'trmet_tourdate_a', true); ?>
<?php if ( ! empty ( $dates_a ) ) { // if it exists, show... ?>
    <div class="tourDate"><?php echo $dates_a; ?></div>
    <a href="<?php echo get_post_meta($post->ID, 'trmet_toururl_a', true); ?>" target="_blank" class="<?php echo get_post_meta($post->ID, 'trmet_tour_status_a', true); ?>"></a>
<?php } ?>

In this code, I need to have it read so that it also looks for _b, _c, _d and all the way to j.

1 Answer 1


You can use PHP's range to iterate through letters.

foreach( range('a', 'j') as $letter ) {
    if( get_post_meta(get_the_ID(), 'trmet_tour_status_' . $letter, true) ){
        echo get_post_meta(get_the_ID(), 'trmet_tour_status_' . $letter, true);
  • Thanks a lot. I used a variation of this for my needs. I’ve never used the range function, and am glad to know it exists. Thanks again! Dec 8, 2015 at 20:40

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.