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');
Gabriel Reguly answers:
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
Adam Bundy comments:
Exactly! Thanks Gabriel. I'll look for your next post. Thanks
Gabriel Reguly comments:
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;
}
Gabriel Reguly comments:
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
Adam Bundy comments:
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!
Gabriel Reguly comments:
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
Adam Bundy comments:
Excellent! That worked Gabriel. Thank you. Thanks to you as well, Arnav.
Adam Bundy comments:
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');
Sébastien | French WordpressDesigner answers:
not sure to understand...
the username must be replaced by the company name. That's it ?
Adam Bundy comments:
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!
Adam Bundy comments:
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?
Sébastien | French WordpressDesigner comments:
i don't understand : where is this menu ?
Adam Bundy comments:
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.
Adam Bundy comments:
...of course, you wont have this post type established- you can test it under posts and I can change that here.
Arnav Joy answers:
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;
}
?>
Adam Bundy comments:
@Arnav, thanks much - could you place this into context amongst my code please?
Arnav Joy comments:
yes , please provide me details of admin or ftp
Adam Bundy comments:
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!
Arnav Joy comments:
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');