Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm sure this is a fairly basic question but I can't seem to get it right.

I have multiple custom field values for addresses in my custom post type and I want to save them as a single value or array so I can display them in one call rather than many. I would also like to output the address comma separated, but first need to preg replace any commas entered by the user in the wordpress backend so I don't end up with, for example, '10 high street,, London,, W11 1TT'

I have this currently:

<?php global $post;
$address_name = get_post_meta( $post->ID, '_mfl_entry_address_name', true );
$address_street = get_post_meta( $post->ID, '_mfl_entry_address_street', true );
$address_line_2 = get_post_meta( $post->ID, '_mfl_entry_address_line_2', true );
$address_line_3 = get_post_meta( $post->ID, '_mfl_entry_address_line_3', true );
$address_postcode = get_post_meta( $post->ID, '_mfl_entry_address_postcode', true );
?>

How would I put these into a single value called 'address', clean out any commas added by the user, then output the address with each value separated by a comma, except the last one?

Thanks for any help

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Updated

    <?php global $post;
    $address_name = str_replace(
        ',', 
        '',
         get_post_meta( $post->ID, '_mfl_entry_address_name', true )
    );

    $address_street = str_replace(
        ',', 
        '',
         get_post_meta( $post->ID, '_mfl_entry_address_street', true )
    );

    $address_line_2 = str_replace(
        ',', 
        '',
         get_post_meta( $post->ID, '_mfl_entry_address_line_2', true )
    );

    $address_line_3 = str_replace(
        ',', 
        '',
         get_post_meta( $post->ID, '_mfl_entry_address_line_3', true )
    );

    $address_postcode = str_replace(
        ',', 
        '',
         get_post_meta( $post->ID, '_mfl_entry_address_postcode', true )
    );

    $full_address = array(
        $address_name, 
        $address_street, 
        $address_line_2,
        $address_line_3,
        $address_postcode
    );

    $output = '';
    $glue = ',';
    foreach($full_address as $value){
        if(!empty($value)){
            $output .= $value.$glue;
        }
    }
    $output = trim($output, $glue);

    echo $output;
    ?>

Let me know if that works.

share|improve this answer
    
+1, except no need to save, he just wanted to output the combined value. –  dalbaeb Jul 23 '13 at 15:05
    
@GhostToast thanks for your reply. That puts everything into a single field nicely but when I output it it contains multiple commas. It doesn't seem to skip the empty fields. And it also doesn't strip out any commas entered by the user –  Xav Jul 23 '13 at 21:16
    
@GhostToast Your edit has sorted the problem nicely, thank you. At risk of asking something that should be posted as separate question, is there an easy way to edit that string replace to remove trailing whitespace at the end of each value? –  Xav Jul 24 '13 at 13:54
    
Check out trim(), which I actually demo in the code above. It can be used to hack off whitespace at the end. Also, if this answer has served you, please mark it as correct. –  GhostToast Jul 24 '13 at 13:57
    
Will do, thanks for your help –  Xav Jul 24 '13 at 14:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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