BP_XProfile_ProfileData::get_value_byid( int $field_id, int|array|null $user_ids = null )
Get profile field values by field ID and user IDs.
Description
Supports multiple user IDs.
Parameters
- $field_id
-
(Required) ID of the field.
- $user_ids
-
(Optional) ID or IDs of user(s).
Default value: null
Return
(string|array) Single value if a single user is queried, otherwise an array of results.
Source
File: bp-xprofile/classes/class-bp-xprofile-profiledata.php
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | public static function get_value_byid( $field_id , $user_ids = null ) { global $wpdb ; if ( empty ( $user_ids ) ) { $user_ids = bp_displayed_user_id(); } $return_single_result = false; if ( ! is_array ( $user_ids ) ) { $return_single_result = true; } $user_ids = wp_parse_id_list( $user_ids ); // Assemble uncached IDs. $uncached_ids = array (); foreach ( $user_ids as $user_id ) { $cache_key = "{$user_id}:{$field_id}" ; if ( false === wp_cache_get( $cache_key , 'bp_xprofile_data' ) ) { $uncached_ids [] = $user_id ; } } // Prime caches. if ( ! empty ( $uncached_ids ) ) { $bp = buddypress(); $uncached_ids_sql = implode( ',' , $uncached_ids ); $queried_data = $wpdb ->get_results( $wpdb ->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id IN ({$uncached_ids_sql})" , $field_id ) ); // Rekey. $qd = array (); foreach ( $queried_data as $data ) { $qd [ $data ->user_id ] = $data ; } foreach ( $uncached_ids as $id ) { // The value was successfully fetched. if ( isset( $qd [ $id ] ) ) { $d = $qd [ $id ]; // No data found for the user, so we fake it to // avoid cache misses and PHP notices. } else { $d = new stdClass; $d ->id = '' ; $d ->user_id = $id ; $d ->field_id = $field_id ; $d ->value = '' ; $d ->last_updated = '' ; } $cache_key = "{$d->user_id}:{$field_id}" ; wp_cache_set( $cache_key , $d , 'bp_xprofile_data' ); } } // Now that the cache is primed with all data, fetch it. $data = array (); foreach ( $user_ids as $user_id ) { $cache_key = "{$user_id}:{$field_id}" ; $data [] = wp_cache_get( $cache_key , 'bp_xprofile_data' ); } // Integer casting. foreach ( ( array ) $data as $key => $d ) { if ( isset( $data [ $key ]->id ) ) { $data [ $key ]->id = (int) $data [ $key ]->id; } if ( isset( $data [ $key ]->user_id ) ) { $data [ $key ]->user_id = (int) $data [ $key ]->user_id; } $data [ $key ]->field_id = (int) $data [ $key ]->field_id; } // If a single ID was passed, just return the value. if ( $return_single_result ) { return $data [0]->value; // Otherwise return the whole array. } else { return $data ; } } |
Changelog
Version | Description |
---|---|
BuddyPress 1.0.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.