Title: wordpress user admin columns
Last modified: November 6, 2020

---

# wordpress user admin columns

 *  [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/)
 * I was wondering if anyone can help me with that. I have a code for admin user
   extra columns:
 *     ```
       function new_modify_user_table( $column ) {
           $column['id'] = 'ID';
           $column['favorites'] = 'Favorites';
           return $column;
       }
       add_filter( 'manage_users_columns', 'new_modify_user_table' );
   
       function new_modify_user_table_row( $val, $column_name, $user_id ) {
           switch ($column_name) {
               case 'id' :
                   return get_the_author_meta( 'ID', $user_id );
               case 'favorites' :
                   return get_the_author_meta('simplefavorites', $user_id);
                   var_dump($posts);
               default:
           }
           return $val;
       }
       add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
       ```
   
 * and my simplefavorites meta looks like this:
 *     ```
       array (
         0 => 
         array (
           0 => 
           array (
             'site_id' => 1,
             'posts' => 
             array (
               0 => 51380,
               1 => 60244,
               2 => 59759,
               3 => 34970,
               4 => 59849,
               5 => 49747,
               6 => 27365,
               7 => 18653,
               8 => 15613,
             ),
             'groups' => 
             array (
               0 => 
               array (
                 'group_id' => 1,
                 'site_id' => 1,
                 'group_name' => 'Default List',
                 'posts' => 
                 array (
                   0 => 51380,
                   1 => 60244,
                   2 => 59759,
                   3 => 34970,
                   4 => 59849,
                   5 => 49747,
                   6 => 27365,
                   7 => 18653,
                   8 => 15613,
                 ),
               ),
             ),
           ),
         ),
       )
       ```
   
 * i would like to display in the column number of posts from the array. Is this
   possible? What can i do? How i can drill down to array with this function?
 * Thanks in advance!
    Alex
    -  This topic was modified 5 years, 7 months ago by [aosipov](https://wordpress.org/support/users/aosipov/).

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/wordpress-user-admin-columns/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wordpress-user-admin-columns/page/2/?output_format=md)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13631264)
 * You need to `count()` the proper array element. Assuming you assign the meta 
   data to `$faves` and everything is an array and not an object, do something like
   `
   echo count( $faves[0][0]['posts']);`
 * Should output `9` for your example var dump.
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13631281)
 * Hey bcworkz, how do i modify my existing function?
 *     ```
       function new_modify_user_table( $column ) {
           $column['id'] = 'ID';
       	$column['favorites'] = 'Favorites';
           return $column;
       }
       add_filter( 'manage_users_columns', 'new_modify_user_table' );
   
       function new_modify_user_table_row( $val, $column_name, $user_id ) {
           switch ($column_name) {
               case 'id' :
                   return get_the_author_meta( 'ID', $user_id );
       	    case 'favorites' :
                   return get_the_author_meta('simplefavorites', $faves);
       			echo count( $faves[0][0]['posts']);
       			var_dump($posts);
               default:
           }
           return $val;
       }
       add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
       ```
   
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13631542)
 * I think i am doing something wrong here LOL:
 *     ```
       function new_modify_user_table( $column ) {
           $column['id'] = 'ID';
       	$column['favorites'] = 'Favorites';
           return $column;
       }
       add_filter( 'manage_users_columns', 'new_modify_user_table' );
   
       function new_modify_user_table_row( $val, $column_name, $user_id ) {
           switch ($column_name) {
               case 'id' :
                   return get_the_author_meta( 'ID', $user_id );
       	    case 'favorites' :
                   return get_the_author_meta('simplefavorites', $user_id);
       			$faves = array('simplefavorites' => array('posts'));
       			echo count( $faves[0][0]['posts']);
               default:
           }
           return $val;
       }
       add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13633541)
 * Argh, my bad, sorry. Change `echo` to `return` please.
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13633938)
 * 🙁 doesn’t work, displays “Array”
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13635412)
 * Oh, I didn’t look at the rest of your code closely enough, the entire favorites
   case is messed up. Should be:
 *     ```
         case 'favorites' :
           $faves = get_the_author_meta('simplefavorites', $user_id);
           return count( $faves[0][0]['posts']);
       ```
   
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13636321)
 * yeey thank you, its working, but it shows 1 or 0, not a number of posts.
    This
   is the code:
 *     ```
       function new_modify_user_table( $column ) {
           $column['id'] = 'ID';
       	$column['favorites'] = 'Favorites';
           return $column;
       }
       add_filter( 'manage_users_columns', 'new_modify_user_table' );
   
       function new_modify_user_table_row( $val, $column_name, $user_id ) {
           switch ($column_name) {
               case 'id' :
                   return get_the_author_meta( 'ID', $user_id );
        case 'favorites' :
           $faves = get_the_author_meta('simplefavorites', $user_id);
           return count( $faves[0][0]['posts']);
               default:
           }
           return $val;
       }
       add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13640123)
 * I tested your code on my site and it works as expected. Well, not exactly, I 
   don’t have “simplefavorites” author meta. I substituted this line for getting
   author meta:
    `$faves = [[['posts'=>[123,234,345]]]];` Which should match in 
   structure the pertinent part of your author meta data. It results in ‘3’ shown
   for every user. We must be getting the ‘posts’ array or you’d be getting a bunch
   of errors.
 * Try changing the return count line to this:
    `return print_r($faves[0][0]['posts'],
   true);` The output should be an indexed array of post IDs. View the page’s HTML
   source to see the full output. Please show me an example of a user’s output for
   this.
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13640644)
 * Hi [@bcworkz](https://wordpress.org/support/users/bcworkz/) when i use this line`
   return count( $faves[0][0]['posts']);`
    my output for user who has simplefavorites
   is `<td class="favorites column-favorites" data-colname="Favorites">0</td>` User
   who doesnt have `<td class="favorites column-favorites" data-colname="Favorites"
   >1</td>` When i replace with this line `return print_r($faves[0][0]['posts'],
   true);` All output is empty.:( `<td class="favorites column-favorites" data-colname
   ="Favorites"></td>`
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13645262)
 * Well, that blows one theory on what’s happening. Lets try
    `return print_r($faves[
   0][0], true);`
 * We gotta get to the right element to count, but it’s sounding like the return
   from get_the_author_meta() isn’t matching the output from your OP. If there’s
   still no output within the td cell, remove one of the `[0]` elements until we
   do get something.
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13650256)
 * Well first one did not work, but when you remove [0]
    `return print_r($faves[
   0], true);` i’ve got output like this:
 *     ```
       <td class="favorites column-favorites" data-colname="Favorites">Array
       (
           [site_id] => 1
           [posts] => Array
               (
                   [0] => 35665
               )
   
           [groups] => Array
               (
                   [0] => Array
                       (
                           [group_id] => 1
                           [site_id] => 1
                           [group_name] => Default List
                           [posts] => Array
                               (
                                   [0] => 35665
                               )
   
                       )
   
               )
   
       )
       </td>
       ```
   
 * basically this is the text that outputs in the table cell:
    _Array ( [site\_id]
   => 1 [posts] => Array ( [0] => 35665 ) [groups] => Array ( [0] => Array ( [group\
   _id] => 1 [site\_id] => 1 [group\_name] => Default List [posts] => Array ( [0]
   => 35665 ) ) ) )_
 * A
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13651098)
 * heeeey i did this
    `return count( $faves[0]['posts']);` this works, Thank you!!!!!
   displays number 1 though when is there is empty array, but thats ok:)
 * Now i am wondering if i can display the IDs of the posts in next column, so to
   display list of those posts IDs.
    This is what i have so far:
 *     ```
       function new_modify_user_table( $column ) {
           $column['id'] = 'ID';
       	$column['favorites_number'] = 'Favorites Number';
       	$column['favorites_posts'] = 'Favorites Posts';
           return $column;
       }
       add_filter( 'manage_users_columns', 'new_modify_user_table' );
   
       function new_modify_user_table_row( $val, $column_name, $user_id ) {
           switch ($column_name) {
       case 'id' :
           return get_the_author_meta( 'ID', $user_id );
       case 'favorites_number' :
           $faves = get_the_author_meta('simplefavorites', $user_id);
           return count( $faves[0]['posts']);
       case 'favorites_posts' :
           $faves = get_the_author_meta('simplefavorites', $user_id);
           return print_r( $faves[0], true);
               default:
           }
           return $val;
       }
       add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
       ```
   
 * Thanks
    A
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13654750)
 * Ha! That bugger had to be there somewhere. A matter of finding the right “address”.
 * Output the same ‘posts’ IDs that we’re counting? “Implode” the array into a string.
   Like so:
    `echo implode(', ', $faves[0]['posts']);`
 * That’s not a good look if this output is empty and the count shows “1”. A quirk
   of `count()` is it’ll return `1` if there is a non-countable item passed to it.
   I believe `implode()` will log warnings in the error log if it’s not passed an
   array. In each case we ought to check if there is something to count or implode
   before doing so. I think checking `is_array()` will work for this. The ternary
   operator `? :`, a shorthand if/else structure is useful in these cases. For example:
   `
   echo is_array( $faves[0]['posts']) ? count( $faves[0]['posts']) :'0';`
 *  Thread Starter [aosipov](https://wordpress.org/support/users/aosipov/)
 * (@aosipov)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13655650)
 * hey [@bcworkz](https://wordpress.org/support/users/bcworkz/)
    this is works:
 *     ```
       function new_modify_user_table( $column ) {
           $column['id'] = 'ID';
       	$column['favorites_number'] = 'Favorites Number';
       	$column['favorites_posts'] = 'Favorites Posts';
           return $column;
       }
       add_filter( 'manage_users_columns', 'new_modify_user_table' );
   
       function new_modify_user_table_row( $val, $column_name, $user_id ) {
           switch ($column_name) {
       case 'id' :
           return get_the_author_meta( 'ID', $user_id );
       case 'favorites_number' :
           $faves = get_the_author_meta('simplefavorites', $user_id);
           return count( $faves[0]['posts']);
       case 'favorites_posts' :
           $faves = get_the_author_meta('simplefavorites', $user_id);
       	return implode(', ', $faves[0]['posts']);
   
               default:
           }
           return $val;
       }
       add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
       ```
   
 * in one column it displays the number of saved posts, in another display array:
   44212, 35940, 43810, 32773, 58370, 43843, 53505, 50357… like this
 * The only thing that is not working is in first column “favorites_number” it displays
   1 when there are no posts selected
 * Thank you so much for your help!
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/#post-13659426)
 * You’re welcome. So close! There’s a solution to the 1 for nothing issue. It’s
   difficult only because I cannot easily fiddle with the code, you have to do it
   for me. Try replacing the return count line in its entirety with this:
    `return
   empty( $faves[0]['posts']) ? '0' : count( $faves[0]['posts']);`
 * In English that means “if the posts array is empty, return ‘0’ otherwise return
   the count”.

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/wordpress-user-admin-columns/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wordpress-user-admin-columns/page/2/?output_format=md)

The topic ‘wordpress user admin columns’ is closed to new replies.

## Tags

 * [admin](https://wordpress.org/support/topic-tag/admin/)
 * [array](https://wordpress.org/support/topic-tag/array/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 17 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 6 months ago](https://wordpress.org/support/topic/wordpress-user-admin-columns/page/2/#post-13662530)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
