global_terms() WordPress Function

The global_terms() function is used to retrieve all global terms for a given taxonomy. Global terms are terms that are shared across all sites in a multisite installation. This function returns an array of term objects.

global_terms( int $term_id, string $deprecated = '' ) #

Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.


Description

Top ↑

See also


Top ↑

Parameters

$term_id

(int)(Required)An ID for a term on the current blog.

$deprecated

(string)(Optional)Not used.

Default value: ''


Top ↑

Return

(int) An ID from the global terms table mapped from $term_id.


Top ↑

Source

File: wp-includes/ms-functions.php

function global_terms( $term_id, $deprecated = '' ) {
	global $wpdb;
	static $global_terms_recurse = null;

	if ( ! global_terms_enabled() ) {
		return $term_id;
	}

	// Prevent a race condition.
	$recurse_start = false;
	if ( null === $global_terms_recurse ) {
		$recurse_start        = true;
		$global_terms_recurse = 1;
	} elseif ( 10 < $global_terms_recurse++ ) {
		return $term_id;
	}

	$term_id = (int) $term_id;
	$c       = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );

	$global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
	if ( null == $global_id ) {
		$used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
		if ( null == $used_global_id ) {
			$wpdb->insert(
				$wpdb->sitecategories,
				array(
					'cat_ID'            => $term_id,
					'cat_name'          => $c->name,
					'category_nicename' => $c->slug,
				)
			);
			$global_id = $wpdb->insert_id;
			if ( empty( $global_id ) ) {
				return $term_id;
			}
		} else {
			$max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
			$max_local_id  = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
			$new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
			$wpdb->insert(
				$wpdb->sitecategories,
				array(
					'cat_ID'            => $new_global_id,
					'cat_name'          => $c->name,
					'category_nicename' => $c->slug,
				)
			);
			$global_id = $wpdb->insert_id;
		}
	} elseif ( $global_id != $term_id ) {
		$local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
		if ( null != $local_id ) {
			global_terms( $local_id );
			if ( 10 < $global_terms_recurse ) {
				$global_id = $term_id;
			}
		}
	}

	if ( $global_id != $term_id ) {
		if ( get_option( 'default_category' ) == $term_id ) {
			update_option( 'default_category', $global_id );
		}

		$wpdb->update( $wpdb->terms, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
		$wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
		$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $global_id ), array( 'parent' => $term_id ) );

		clean_term_cache( $term_id );
	}
	if ( $recurse_start ) {
		$global_terms_recurse = null;
	}

	return $global_id;
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More