Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/19/2016 11:44:15 PM (8 years ago)
Author:
dcavins
Message:

Add multisite-aware wrapper for get_term_by().

Add wrapper functions for working with BuddyPress taxonomies via

get_term_by(). When working with BuddyPress objects like users, we
need to be sure that we’re referring to the taxonomies and terms for
the site on the network where the relevant taxonomy is registered.

Props dcavins, boonebgorges, offereins.

Fixes #7263.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-taxonomy.php

    r11118 r11119  
    241241}
    242242
     243/**
     244 * Get term data for terms in BuddyPress taxonomies.
     245 *
     246 * Note that term data is from the `bp_get_taxonomy_term_site_id()`, which on some
     247 * multisite configurations may not be the same as the current site.
     248 *
     249 * @since 2.7.0
     250 *
     251 * @see get_term_by() for a full description of function and parameters.
     252 *
     253 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
     254 * @param string|int $value    Search for this term value
     255 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
     256 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
     257 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
     258 *
     259 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
     260 *                      or `$term` was not found.
     261 */
     262function bp_get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
     263    $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     264
     265    $switched = false;
     266    if ( $site_id !== get_current_blog_id() ) {
     267        switch_to_blog( $site_id );
     268        bp_register_taxonomies();
     269        $switched = true;
     270    }
     271
     272    $term = get_term_by( $field, $value, $taxonomy, $output, $filter );
     273
     274    if ( $switched ) {
     275        restore_current_blog();
     276    }
     277
     278    return $term;
     279}
Note: See TracChangeset for help on using the changeset viewer.