How can I get WordPress to pull all description metadata fields without knowing their names or length?

westcoast_509 :

How can I get WordPress to find and display all of the descriptions in use for a user? Typically, a user on our site will have a description in 2-3 languages. The additional description fields in other languages are created by a plugin (and manually filled in by our editors). Currently, ANOTHER plugin uses this code to display the main description for a user:

$biography = get_the_author_meta( 'description', $user->user_id )

However, I need it to pull ALL description fields such as:

$biography = get_the_author_meta( 'description', $user->user_id ) . $biography = get_the_author_meta( 'description_zh', $user->user_id )

But this is clunky, and I won't know ahead of time how many languages will be added to the site. I thought maybe something like this might work, but no luck:

$biography = get_the_author_meta( 'description.*', $user->user_id )

I'm new to PHP, so any help much appreciated!

fxrgc :
$bio_meta = (array)get_user_meta( $user->user_id );

foreach ( $bio_meta as $key => $value_array ) {
    if ( strpos( $key, 'description' ) !== false ) {
        $description_values[] = $value_array[0];
    }
}
$biography = implode( ' ', $description_values );

Turning the meta results into an array makes life easier, then just check each key that begins with "description."

If you wanted the output to include the key, you could change

$description_values[] = $value_array[0];

into this:

$description_values[] = array( $key => $value_array[0] );

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=198418&siteId=1