mysql - Combine meta values into a new key (PHP/WordPress) -


here's need do, in laymen's terms:

for each wordpress user, find every meta key starts "foo_" combine meta values new key named "foo_combined"

my question refers wordpress, think applies enough php or sql, hope proper forum.

i understand, related answers found, need this:

function combine_keys() {      $array = // array of (all?) user meta      foreach ($array $key => $value) {         if (substr($key, 0, 4) == "foo_") {             // grab $value , append key??         }     }  } 

but don't know how translate wordpress functions. know get_users() users. , get_user_meta($user_id) user meta specific user id. don't know how implement them above function. have lot of users, needs done efficiently.

my other question when call function. purpose of exporting csv. i'm using user export plugin, need merge foo_ keys, values separated line break, single key don't have export million foo_ columns.

** update **

here updated code trying use implement answer below. put functions.php.

function combine_keys( $user_id ) {      $array    = array();     $all_meta = get_user_meta( $user_id );      foreach( $all_meta $key => $meta ) {         if( preg_match('/^workshop_/', $key) )             $array[$key] = $meta[0];     }      return $array; }  add_action('personal_options_update', function( $user_id ){     if ( current_user_can('edit_user', $user_id) )         update_user_meta( $user_id, 'workshop_combined', combine_keys( $user_id ) );  }); 

this works create new key (called workshop_combined) , sucessfully combines values array this:

a:2:{s:10:"workshop_5";s:43:"test, may 15, 2015, score: 80, hours: 30 ";s:11:"workshop_30";s:68:"middle school civics & economics, jun 04, 2015, score: 12, hours: 43";}

but there 2 problems:

  1. everytime update profile, adds combined value workshop_combined key, rather replacing value. though says update_meta , not add_meta. know why happening?

  2. i'd turn array string. instead of "return $array", tried implode this:

    $comma_separated = implode(",", $array); return $comma_separated;

but returns nothing, , value empty. have done wrong? thank you!

here is:

function combine_keys( $user_id ) {      $array    = array();     $all_meta = get_user_meta( $user_id );      foreach( $all_meta $key => $meta ) {         if( preg_match('/^foo_/', $key) )             $array[$key] = $meta[0];     }      return $array; } 

you can create combined key this:

foreach (get_users() $user) {     $combined_meta = combine_keys( $user->id );     update_user_meta( $user->id, 'foo_combined', $combined_meta ); } 

as when call it, right before exporting csv (maybe using hook provided plugin), or schedule it when want.

or let single user save personal combined key when updates profile:

add_action('personal_options_update', function( $user_id ){     if ( current_user_can('edit_user', $user_id) )         update_user_meta( $user_id, 'foo_combined', combine_keys( $user_id ) ); }); 

update

answering new questions:

  1. that happens because foo_combined starts foo_, it's added array (sorry, fault :). replacing search pattern /^foo_(?!combined)/ exclude key.
  2. it should working, don't see why doesn't.

i've updated code meet new requirements , tested it:

function combine_keys( $user_id ) {      $array    = array();     $all_meta = get_user_meta( $user_id );      foreach( $all_meta $key => $meta ) {         if( preg_match('/^foo_(?!combined)/', $key) )             $array[$key] = $meta[0];     }      return implode(',', $array); }  add_action('personal_options_update', function( $user_id ){     if ( current_user_can('edit_user', $user_id) )         update_user_meta( $user_id, 'foo_combined', combine_keys( $user_id ) );  }); 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -