• Resolved dibritto

    (@dibritto)


    Good afternoon everyone, this is my first post here in the community and would help staff to understand about WordPress database, already scoured the internet and found nothing like what I need, good in my case I am using wordpress clean without modifications, and specifically without plug-ins for this is my goal, however I am not getting satisfactory results, I can write and delete the wp-usermeta table, I have several meta_key fields recorded meta_value a single user_id however when I do a query in meta_key fields to return me the values ​​of each meta_value apena he repeats the first result of meta_value and not listing the other fields of the same user_id, would like a solution, if anyone can help I will be grateful …

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello and welcome!

    Try using get_user_meta(). There are a number of basic examples in the user notes at the bottom of the page.

    Thread Starter dibritto

    (@dibritto)

    This link does not clearly show how to get a query in 2 or more mata_value, the code I use is:

    
             <?php
                     // Lista os Usuarios que realizaram deposito
                     $user_query = new WP_User_Query( 
                     
                     array( 
                     'meta_key'	=> 'ma_deposito', 
                     'user_id'	=> 'ma_deposito', 
                     'orderby'	=> 'meta_value', 
                     'order'		=> 'ASC', 
                     'number'	=> 10
                     ) );// coluna e valor da coluna
                     
                        
                     // User Loop
                         if ( ! empty( $user_query->results ) ) {
                                 _e('<div class="table-responsive">');
                                     _e('<table class="table table-striped table-bordered table-hover  table-condensed">');
                                             _e('<tr>');
                                                 _e('<td class="active"><strong>Apelido</strong></td><td class="active"><strong>E-mail</strong></td><td class="active"><strong>Valor</strong></td>');
                                             _e('</tr>');
                             foreach ( $user_query->results as $user ):
                                             _e('<tr>');
                                                 _e( '<td class="">'.$user->user_nicename.'</td><td class="">'.$user->user_email.'</td><td class="'.$user->ma_deposito_css.'">R$ ' . $user->ma_deposito .',00</td>');
                                             _e('</tr>');
                             endforeach;
                                         _e('</tr>');
                                     _e('</table>');
                     
                     echo('<span class="pull-right"><strong>Total arrecadado:</strong> R$ '.$valortotal.',00 reais</span>');
                     
                                 _e('</div>');
                     
                         } else {
                                 echo '
                                     <div class="table-responsive">
                                         <table class="table">
                                             <tr>
                                                 <td class="page-header alert alert-info text-center">Ainda não existe colaborações para este evento, seja o primeiro colaborador!. </td>
                                             </tr>
                                         </table>
                                     </div>
                                 ';}?>

    Here is an example of a user experiencing the same problem as me:

    http://wordpress.stackexchange.com/questions/212190/issue-with-foreach-on-duplicate-meta-keys

    Moderator bcworkz

    (@bcworkz)

    You can either get all meta data for a user or that of a specific key. Within the user loop, get all of the user’s meta data with something like this:
    $all_data = get_user_meta( $user->ID );

    You could loop through the returned array and output every field, but there’s no telling what kind of data might be in the array. Most prefer to selectively output only certain values, for example:
    echo "User's real name (last, first): {$all_data['last_name']}, {$all_data['first_name']}<br>\n";

    Unless you are sure the specific meta key exists for all users, you should verify it’s existence first with array_key_exists().

    BTW, I would suggest you not run HTML strings through translation functions like _e(). For example _e('</table>');. While it does end up echoing out the string, it’s not very efficient. Just use the echo statement in most cases. Even for strings that are translatable, no translation will occur unless the string is pre-defined in the default translation file. If you are writing a plugin that will be eventually translated, you need to specify your plugin’s text domain in all translation calls.

    Thread Starter dibritto

    (@dibritto)

    hi @bcworkz

    Thanks for your help, I was able to solve it by creating a query and a LEFT JOIN, thanks for your help.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘query repeated in the column meta_value’ is closed to new replies.