Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/08/2022 06:27:01 AM (2 years ago)
Author:
imath
Message:

xProfile: allow the loop to fetch fields from multiple fields group

You can now pass an array of fields group ID into the $profile_group_id
parameter.

Props boonebgorges, espellcaste

Closes https://github.com/buddypress/buddypress/pull/30
Fixes #4075

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r13108 r13358  
    238238     *
    239239     * @since 1.2.0
    240      * @since 2.4.0 Introduced `$member_type` argument.
    241      * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     240     * @since 2.4.0  Introduced `$member_type` argument.
     241     * @since 8.0.0  Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     242     * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
    242243     *
    243244     * @global object $wpdb WordPress DB access object.
     
    245246     * @param array $args {
    246247     *      Array of optional arguments.
    247      *      @type int          $profile_group_id   Limit results to a single profile group.
    248      *      @type int          $user_id            Required if you want to load a specific user's data.
    249      *                                             Default: displayed user's ID.
    250      *      @type array|string $member_type        Limit fields by those restricted to a given member type, or array of
    251      *                                             member types. If `$user_id` is provided, the value of `$member_type`
    252      *                                             will be overridden by the member types of the provided user. The
    253      *                                             special value of 'any' will return only those fields that are
    254      *                                             unrestricted by member type - i.e., those applicable to any type.
    255      *      @type bool         $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
    256      *      @type bool         $hide_empty_fields  True to hide fields where the user has not provided data.
    257      *                                             Default: false.
    258      *      @type bool         $fetch_fields       Whether to fetch each group's fields. Default: false.
    259      *      @type bool         $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
    260      *                                             Default: false.
    261      *      @type int[]|bool   $exclude_groups     Comma-separated list or array of group IDs to exclude.
    262      *      @type int[]|bool   $exclude_fields     Comma-separated list or array of field IDs to exclude.
    263      *      @type string[]     $hide_field_types   List of field types to hide form loop. Default: empty array.
    264      *      @type bool         $signup_fields_only Whether to only return signup fields. Default: false.
    265      *      @type bool         $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
    266      *                                             and data. Default: true.
     248     *
     249     *      @type int|int[]|bool $profile_group_id   Limit results to a single profile group or a comma-separated list or array of
     250     *                                               profile group ids. Default: false.
     251     *      @type int            $user_id            Required if you want to load a specific user's data.
     252     *                                               Default: displayed user's ID.
     253     *      @type array|string   $member_type        Limit fields by those restricted to a given member type, or array of
     254     *                                               member types. If `$user_id` is provided, the value of `$member_type`
     255     *                                               will be overridden by the member types of the provided user. The
     256     *                                               special value of 'any' will return only those fields that are
     257     *                                               unrestricted by member type - i.e., those applicable to any type.
     258     *      @type bool           $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
     259     *      @type bool           $hide_empty_fields  True to hide fields where the user has not provided data.
     260     *                                               Default: false.
     261     *      @type bool           $fetch_fields       Whether to fetch each group's fields. Default: false.
     262     *      @type bool           $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
     263     *                                               Default: false.
     264     *      @type int[]|bool     $exclude_groups     Comma-separated list or array of group IDs to exclude.
     265     *      @type int[]|bool     $exclude_fields     Comma-separated list or array of field IDs to exclude.
     266     *      @type string[]       $hide_field_types   List of field types to hide form loop. Default: empty array.
     267     *      @type bool           $signup_fields_only Whether to only return signup fields. Default: false.
     268     *      @type bool           $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
     269     *                                               and data. Default: true.
    267270     * }
    268      * @return array $groups
     271     * @return array
    269272     */
    270273    public static function get( $args = array() ) {
     
    463466     *
    464467     * @since 5.0.0
     468     * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
    465469     *
    466470     * @param array $args {
    467      *    Array of optional arguments:
    468      *    @type int   $profile_group_id  Limit results to a single profile group. Default false.
    469      *    @type array $exclude_groups    Comma-separated list or array of group IDs to exclude. Default false.
    470      *    @type bool  $hide_empty_groups True to hide groups that don't have any fields. Default: false.
     471     *    Array of optional arguments.
     472     *
     473     *    @type int|int[]|bool $profile_group_id  Limit results to a single profile group or a comma-separated list or array of
     474     *                                       profile group ids. Default: false.
     475     *    @type int[]          $exclude_groups    Comma-separated list or array of group IDs to exclude. Default: false.
     476     *    @type bool           $hide_empty_groups True to hide groups that don't have any fields. Default: false.
    471477     * }
    472478     * @return array
     
    486492        $bp = buddypress();
    487493
    488         if ( ! empty( $r['profile_group_id'] ) ) {
    489             $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
     494        if ( ! empty( $r['profile_group_id'] ) && ! is_bool( $r['profile_group_id'] ) ) {
     495            $profile_group_ids = join( ',', wp_parse_id_list( $r['profile_group_id'] ) );
     496            $where_sql         = "WHERE g.id IN ({$profile_group_ids})";
    490497        } elseif ( $r['exclude_groups'] ) {
    491498            $exclude   = join( ',', wp_parse_id_list( $r['exclude_groups'] ) );
Note: See TracChangeset for help on using the changeset viewer.