Come ottenere utenti per un campo personalizzato o metadati utente?
Ho aggiunto un campo personalizzato al profilo utente utilizzando il seguente codice:
/*** Aggiunta di un campo extra per ottenere l'utente che ha creato un altro utente durante AGGIUNGI NUOVO UTENTE ***/
<?php
function custom_user_profile_fields($user){
if(is_object($user))
$created_by = esc_attr( get_the_author_meta( 'created_by', $user->ID ) );
else
$created_by = null;
?>
<h3>Informazioni extra del profilo</h3>
<table class="form-table">
<tr>
<th><label for="created_by">Creato Da</label></th>
<td>
<input type="text" class="regular-text" name="created_by" value="<?php echo $created_by; ?>" id="created_by" /><br />
<span class="description">La persona che ha creato questo utente</span>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );
function save_custom_user_profile_fields($user_id){
update_user_meta($user_id, 'created_by', $_POST['created_by']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
?>
Ora vedo un campo creato da quando creo un nuovo utente dal pannello di amministrazione
E ora voglio ottenere gli utenti in base al campo created_by
Codex: https://codex.wordpress.org/Function_Reference/get_users
Quindi, secondo il codex, dovrebbe essere qualcosa del genere:
<?php
get_users( $args );
$args = array(
'meta_key' => '',
'meta_value' => '',
)
$blogusers = get_users( $args );
// Array di oggetti stdClass.
foreach ( $blogusers as $my_users ) {
echo $my_users. '<br/>';
}
?>
Ho provato varie opzioni per meta_key
e meta_value
Ma tutte restituiscono solo valori vuoti.
Qual è l'esatto meta_key
e meta_value
per il campo che ho creato usando la funzione custom_user_profile_fields
?
Come posso ottenere gli utenti in base al campo personalizzato created_by
?

Qual è l'esatto meta_key e meta_value per il campo che ho creato utilizzando la funzione custom_user_profile_fields?
created_by
e un ID utente, ad esempio:
$args = array(
'meta_key' => 'created_by',
'meta_value' => 123,
)
Potresti utilizzare una meta_query
per ricerche più complesse.
$args = array(
'meta_query' => array(
array(
'key' => 'created_by',
'compare' => 'EXISTS',
),
)
);
var_dump(get_users($args));
In sostanza, comunque, quello che stai facendo è corretto.
