logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$15
Modify 'filter by author' function- use added user meta in label

Credit to Trevor Morris (http://forrst.com/posts/WordPress_Custom_Post_Types_Filter_by_Author_in-s9p#comment-581453) for this functionality that works brilliantly to show (in the posts list - for a custom post type - in the admin) a select menu displaying users' usernames and the value for each option is the user ID #. I would like to modify it to show an expanded user meta field ("company_name" in this case) as the option label. The user ID as the value for each can stay the same. Could anyone help with generating this? Thanks in advance!


function restrict_manage_authors() {
if (isset($_GET['post_type']) && post_type_exists($_GET['post_type']) && in_array(strtolower($_GET['post_type']), array('contestentry'))) {
wp_dropdown_users(array(
'show_option_all' => 'Members:',
'show_option_none' => false,
'name' => 'author',
'selected' => !empty($_GET['author']) ? $_GET['author'] : 0,
'include_selected' => false
));
}
}
add_action('restrict_manage_posts', 'restrict_manage_authors');

function custom_columns_author($columns) {
$columns['author'] = 'Author';
return $columns;
}
add_filter('manage_edit-contestentry_columns', 'custom_columns_author');

This question has been answered.

Adam Bundy | 05/15/12 at 1:25pm Edit

Previous versions of this question: 05/15/12 at 5:12pm | 05/16/12 at 5:16pm

(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    05/15/12
    3:01pm
    Sébastien | French WordpressDesigner says:

    not sure to understand...
    the username must be replaced by the company name. That's it ?

    • 05/15/12 3:06pm

      Adam Bundy says:

      Yes, that's correct. I just want the label visible on the select menu to be the extended user meta with key "company_name" instead of the username. Thanks!

    • 05/15/12 5:11pm

      Adam Bundy says:

      To clarify, this will require a query within this loop of the extra meta field "company_name" for each user listed in the menu and make the text for each option element the company_name value instead of the username value. Make sense?

    • 05/16/12 7:12am

      Sébastien | French WordpressDesigner says:

      i don't understand : where is this menu ?

    • 05/16/12 10:32am

      Adam Bundy says:

      Sebastien, this is a function that creates a select menu above the admin list of posts of type "Contest Entries". It filters the list of posts by author. The client would like to see the author's 'company_name' in the select menu rather than author username. Try the function in a test install to see what I mean.

    • 05/16/12 10:35am

      Adam Bundy says:

      ...of course, you wont have this post type established- you can test it under posts and I can change that here.

  • avatar
    Last edited:
    05/17/12
    7:56am
    Gabriel Reguly says:

    Hi Adam,

    You will need to filter the wp_dropdown_users result to replace the user name with the company name.

    Something like this code


    add_filter('wp_dropdown_users', 'wp_dropdown_users_filter');

    function wp_dropdown_users_filter( $output ) {
    // Here you will need to replace the option text, e.g. <option value="1">User name</option> becomes <option value="1">Company name</option>
    return $output;
    }


    I can code it for you tomorrow.

    Regards,
    Gabriel



    • 05/17/12 10:27am

      Adam Bundy says:

      Exactly! Thanks Gabriel. I'll look for your next post. Thanks

    • 05/17/12 9:01pm

      Gabriel Reguly says:

      function restrict_manage_authors() {

      if (isset($_GET['post_type']) && post_type_exists($_GET['post_type']) && in_array(strtolower($_GET['post_type']), array('contestentry'))) {

      wp_dropdown_users(array(

      'show_option_all' => 'Members:',

      'show_option_none' => false,

      'name' => 'author',

      'selected' => !empty($_GET['author']) ? $_GET['author'] : 0,

      'include_selected' => false

      ));

      }

      }

      add_action('restrict_manage_posts', 'restrict_manage_authors');




      function custom_columns_author($columns) {

      $columns['author'] = 'Author';

      return $columns;

      }

      add_filter('manage_edit-contestentry_columns', 'custom_columns_author');

      add_filter('wp_dropdown_users', 'wp_dropdown_users_modified');
      function wp_dropdown_users_modified( $args = '' ) {
      $siteusers = get_users('orderby=nicename&order=ASC'); // you can pass filters and option

      $re = '';
      $key = 'company_name'; // replave user meta field here
      if (count($siteusers) > 0){
      $re = '<select name="post_author_override" id="post_author_override">';
      foreach ($siteusers as $user) {
      $companyName = get_user_meta($user->ID, $key, true);
      $re .= '<option value="' . $user->ID . '">'.$companyName.'</option>';
      }
      $re .= '</select>';

      }
      return $re;
      }


    • 05/17/12 9:11pm

      Gabriel Reguly says:

      Sorry, last post was incorrectly submitted.

      Please try this one:


      function restrict_manage_authors() {

      if (isset($_GET['post_type']) && post_type_exists($_GET['post_type']) && in_array(strtolower($_GET['post_type']), array('contestentry'))) {
      wp_dropdown_users(array(
      'show_option_all' => 'Members:',
      'show_option_none' => false,
      'name' => 'author',
      'selected' => !empty($_GET['author']) ? $_GET['author'] : 0,
      'include_selected' => false
      ));
      }
      }

      add_action('restrict_manage_posts', 'restrict_manage_authors');


      function custom_columns_author($columns) {
      $columns['author'] = 'Author';
      return $columns;
      }

      add_filter('manage_edit-contestentry_columns', 'custom_columns_author');

      function wp_dropdown_users_modified( $args = '' ) {
      $siteusers = get_users('orderby=nicename&order=ASC');
      $re = '';
      $key = 'company_name'; // replave user meta field here
      if (count($siteusers) > 0){
      $re = '<select name="author" id="post_author_override">';
      foreach ($siteusers as $user) {
      $companyName = get_user_meta($user->ID, $key, true);
      if ( isset( $_GET['author'] ) && ( $user->ID == $_GET['author'] ) {
      $re .= '<option value="' . $user->ID . '" selected="selected">'.$companyName.'</option>';
      } else {
      $re .= '<option value="' . $user->ID . '">'.$companyName.'</option>';
      }
      }
      $re .= '</select>';
      }
      return $re;
      }

      add_filter('wp_dropdown_users', 'wp_dropdown_users_modified');




      Hopefully this will do what you are looking for.

      Regards,
      Gabriel

    • 05/18/12 2:02pm

      Adam Bundy says:

      Gabriel,
      Tried your above code, and ended up with white screen in the admin. It appears that maybe there's a syntax error in this if/then:


      if ( isset( $_GET['author'] ) && ( $user->ID == $_GET['author'] ) {
      $re .= '<option value="' . $user->ID . '" selected="selected">'.$companyName.'</option>';
      } else {
      $re .= '<option value="' . $user->ID . '">'.$companyName.'</option>';
      }


      ??

      Thanks!

    • 05/18/12 3:12pm

      Gabriel Reguly says:

      Hi,

      Indeed, it was missing a ')'.

      Follows amended code.


      if ( isset( $_GET['author'] ) && ( $user->ID == $_GET['author'] ) ) {

      $re .= '<option value="' . $user->ID . '" selected="selected">'.$companyName.'</option>';

      } else {

      $re .= '<option value="' . $user->ID . '">'.$companyName.'</option>';

      }


      Regards,
      Gabriel

    • 05/18/12 3:22pm

      Adam Bundy says:

      Excellent! That worked Gabriel. Thank you. Thanks to you as well, Arnav.

    • 05/18/12 3:23pm

      Adam Bundy says:

      For the record, the final functions.php code ended up being:


      function restrict_manage_authors() {

      if (isset($_GET['post_type']) && post_type_exists($_GET['post_type']) && in_array(strtolower($_GET['post_type']), array('contestentry'))) {

      wp_dropdown_users(array(
      'show_option_all' => 'Members:',
      'show_option_none' => false,
      'name' => 'author',
      'selected' => !empty($_GET['author']) ? $_GET['author'] : 0,
      'include_selected' => false
      ));
      }

      }


      add_action('restrict_manage_posts', 'restrict_manage_authors');

      function custom_columns_author($columns) {
      $columns['author'] = 'Author';
      return $columns;
      }

      add_filter('manage_edit-contestentry_columns', 'custom_columns_author');

      function wp_dropdown_users_modified( $args = '' ) {

      $siteusers = get_users('orderby=nicename&order=ASC');
      $re = '';
      $key = 'acs_company_name';

      if (count($siteusers) > 0){

      $re = '<select name="author" id="post_author_override">';

      foreach ($siteusers as $user) {

      $companyName = get_user_meta($user->ID, $key, true);

      if ( isset( $_GET['author'] ) && ( $user->ID == $_GET['author'] ) ) {
      $re .= '<option value="' . $user->ID . '" selected="selected">'.$companyName.'</option>';
      } else {
      $re .= '<option value="' . $user->ID . '">'.$companyName.'</option>';
      }
      }

      $re .= '</select>';

      }
      return $re;
      }

      add_filter('wp_dropdown_users', 'wp_dropdown_users_modified');

  • avatar
    Last edited:
    05/17/12
    2:10pm
    Arnav Joy says:

    please try this code in functions.php

    <?php

    add_filter('wp_dropdown_users', 'wp_dropdown_users_modified');
    function wp_dropdown_users_modified( $args = '' ) {
    $siteusers = get_users('orderby=nicename&order=ASC'); // you can pass filters and option

    $re = '';
    $key = 'company_name'; // replave user meta field here
    if (count($siteusers) > 0){
    $re = '<select name="post_author_override" id="post_author_override">';
    foreach ($siteusers as $user) {
    $companyName = get_user_meta($user->ID, $key, true);
    $re .= '<option value="' . $user->ID . '">'.$companyName.'</option>';
    }
    $re .= '</select>';

    }
    echo $re;
    }

    ?>

    • 05/17/12 2:24pm

      Adam Bundy says:

      @Arnav, thanks much - could you place this into context amongst my code please?

    • 05/18/12 12:24pm

      Arnav Joy says:

      yes , please provide me details of admin or ftp

    • 05/18/12 2:03pm

      Adam Bundy says:

      Arnav, sorry, I meant just put it into my existing filter users function (as Gabriel did above), not just the custom portion for the company name. Thanks!

    • 05/18/12 2:17pm

      Arnav Joy says:

      try this

      function restrict_manage_authors() {



      if (isset($_GET['post_type']) && post_type_exists($_GET['post_type']) && in_array(strtolower($_GET['post_type']), array('page'))) {

      wp_dropdown_users(array(

      'show_option_all' => 'Members:',

      'show_option_none' => false,

      'name' => 'author',

      'selected' => !empty($_GET['author']) ? $_GET['author'] : 0,

      'include_selected' => false

      ));

      }

      }



      add_action('restrict_manage_posts', 'restrict_manage_authors');





      function custom_columns_author($columns) {

      $columns['author'] = 'Author';

      return $columns;

      }



      add_filter('manage_edit-contestentry_columns', 'custom_columns_author');



      function wp_dropdown_users_modified( $args = '' ) {

      $siteusers = get_users('orderby=nicename&order=ASC');

      $re = '';

      $key = 'company_name'; // replave user meta field here

      if (count($siteusers) > 0){

      $re = '<select name="author" id="post_author_override">';

      foreach ($siteusers as $user) {

      $companyName = get_user_meta($user->ID, $key, true);

      if ( isset( $_GET['author'] ) && ( $user->ID == $_GET['author'] )) {

      $re .= '<option value="' . $user->ID . '" selected="selected">'.$companyName.'</option>';

      } else {

      $re .= '<option value="' . $user->ID . '">'.$companyName.'</option>';

      }

      }

      $re .= '</select>';

      }

      return $re;

      }



      add_filter('wp_dropdown_users', 'wp_dropdown_users_modified');

This question has expired.



Adam Bundy, idt voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.