Tagged: 

Viewing 2 reply threads
  • Author
    Posts
    • #6243
      bshep
      Member

      I’m using the following to generate a select list of my users:

      ,’choices’ => array(
      ” => ‘- Select -‘
      )
      + piklist(
      get_users(
      array(
      ‘orderby’ => ‘last_name’
      ,’order’ => ‘asc’
      )
      ,’objects’
      )
      ,array(
      ‘ID’
      ,’user_login’
      )
      )

      Is there a way to customize the choice label using user metadata? For example, instead of displaying the ‘user_login’ for the select label, I’d like to use ‘last_name, first_name (user_login)’.

    • #6247
      Steve
      Keymaster

      You are limited to the return values of get_users() / WP_User_Query

      ID
      display_name
      user_login
      user_nicename
      user_email
      user_url
      user_registered

    • #6248
      bshep
      Member

      Thanks – I actually just figured this out by modifying some code I used with CMB2 previously. I added the following to the top of my PikList metafields file:

      $args = array(
      ‘blog_id’ => $GLOBALS[‘blog_id’],
      ‘meta_key’ => ‘last_name’,
      ‘orderby’ => ‘meta_value’,
      ‘order’ => ‘ASC’,
      ‘fields’ => ‘all’,
      );

      $entries = get_users( $args );

      $user_options = array();
      if ( $entries ) {
      foreach ( $entries as $entry ) {
      $name = get_user_meta($entry->ID,’last_name’,’single’) . ‘, ‘ . get_user_meta($entry->ID,’first_name’,’single’) . ‘ (‘ . $entry->user_login . ‘)’;
      $user_options[ $entry->ID ] = $name;
      }
      }

      Then referenced it in the select list with:

      ,’choices’ => array(” => ‘- Select -‘) + $user_options

      This gave me the format I needed.

Viewing 2 reply threads
  • The topic ‘meta data used in dynamic select choices’ is closed to new replies.