1

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

1 Answer 1

2

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.

4
  • +1, except no need to save, he just wanted to output the combined value. Jul 23, 2013 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, 2013 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, 2013 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, 2013 at 13:57

Your Answer

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

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