• 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 4 years, 4 months ago by aosipov.
Viewing 15 replies - 1 through 15 (of 17 total)
  • Moderator bcworkz

    (@bcworkz)

    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

    (@aosipov)

    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

    (@aosipov)

    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

    (@bcworkz)

    Argh, my bad, sorry. Change echo to return please.

    Thread Starter aosipov

    (@aosipov)

    ?? doesn’t work, displays “Array”

    Moderator bcworkz

    (@bcworkz)

    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

    (@aosipov)

    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

    (@bcworkz)

    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

    (@aosipov)

    Hi @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

    (@bcworkz)

    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

    (@aosipov)

    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

    (@aosipov)

    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

    (@bcworkz)

    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

    (@aosipov)

    hey @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

    (@bcworkz)

    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)
  • The topic ‘wordpress user admin columns’ is closed to new replies.