Home > other >  using wpdb > get a list of users that are associated with taxonomy
using wpdb > get a list of users that are associated with taxonomy

Time:10-30

i would like to get a list of user ID that are associated with a taxonomy_id = 1186

I got the result from the following wpdb function (I need to use wpdb for some reasons)

$result = $wpdb->get_results( "SELECT u.ID FROM wp_users u INNER JOIN wp_term_relationships r ON u.ID = r.object_id WHERE u.user_status = 0 AND r.term_taxonomy_id = 1186");

but the result I get shows this way https://snipboard.io/otBFli.jpg

Array ( [0] => stdClass Object ( [ID] => 27 ) [1] => stdClass Object ( [ID] => 50 ) [2] => stdClass Object ( [ID] => 148 ) [3] => stdClass Object ( [ID] => 160 ) [4] => stdClass Object ( [ID] => 212 ) [5] => stdClass Object ( [ID] => 234 ) [6] => stdClass Object ( [ID] => 263 ) [7] => stdClass Object ( [ID] => 308 ) [8] => stdClass Object ( [ID] => 352 ) [9] => stdClass Object ( [ID] => 400 ) [10] => stdClass Object ( [ID] => 433 ) [11] => stdClass Object ( [ID] => 449 ) [12] => stdClass Object ( [ID] => 465 ) [13] => stdClass Object ( [ID] => 588 ) [14] => stdClass Object ( [ID] => 604 ) [15] => stdClass Object ( [ID] => 674 ) [16] => stdClass Object ( [ID] => 677 ) [17] => stdClass Object ( [ID] => 759 ) [18] => stdClass Object ( [ID] => 949 ) [19] => stdClass Object ( [ID] => 1143 ) [20] => stdClass Object ( [ID] => 1166 ) [21] => stdClass Object ( [ID] => 1178 ) [22] => stdClass Object ( [ID] => 1180 ) [23] => stdClass Object ( [ID] => 1565 ) [24] => stdClass Object ( [ID] => 1590 ) [25] => stdClass Object ( [ID] => 1786 ) [26] => stdClass Object ( [ID] => 1992 ) [27] => stdClass Object ( [ID] => 2133 ) [28] => stdClass Object ( [ID] => 2312 ) [29] => stdClass Object ( [ID] => 2380 ) [30] => stdClass Object ( [ID] => 3250 ) [31] => stdClass Object ( [ID] => 3678 ) [32] => stdClass Object ( [ID] => 4909 ) [33] => stdClass Object ( [ID] => 5766 ) [34] => stdClass Object ( [ID] => 5767 ) [35] => stdClass Object ( [ID] => 5824 ) [36] => stdClass Object ( [ID] => 6076 ) [37] => stdClass Object ( [ID] => 6461 ) [38] => stdClass Object ( [ID] => 6660 ) [39] => stdClass Object ( [ID] => 6702 ) [40] => stdClass Object ( [ID] => 6723 ) [41] => stdClass Object ( [ID] => 6829 ) [42] => stdClass Object ( [ID] => 6877 ) [43] => stdClass Object ( [ID] => 7112 ) ) Array ( [0] => stdClass Object ( [ID] => 5766 ) )

but I would like to echo this result out into following way 27,50,148,160,212,234.....5766

How do i achieve this?

Thank you!

CodePudding user response:

Try the below code.

$all_ids = array();

$result = $wpdb->get_results( "SELECT u.ID FROM wp_users u INNER JOIN wp_term_relationships r ON u.ID = r.object_id WHERE u.user_status = 0 AND r.term_taxonomy_id = 1186", ARRAY_A );

foreach ( $result as $key => $id ) {
    $all_ids[] = $id['ID'];
}

if( !empty( $all_ids ) ){
    echo implode( ',', $all_ids );
}
  • Related