acf_upgrade_550_taxonomy()ACF 5.7.4

acf_upgrade_550_taxonomy

Upgrades all ACF4 termmeta for a specific taxonomy.

Возвращает

null. Ничего.

Использование

acf_upgrade_550_taxonomy( $taxonomy );
$taxonomy(строка) (обязательный)
The taxonomy name.

Список изменений

С версии 5.7.4 Введена.

Код acf_upgrade_550_taxonomy() ACF 5.10.2

function acf_upgrade_550_taxonomy( $taxonomy ) {

	// log
	acf_dev_log( 'ACF Upgrade 5.5.0 Taxonomy.', $taxonomy );

	// global
	global $wpdb;

	// vars
	$search  = $taxonomy . '_%';
	$_search = '_' . $search;

	// escape '_'
	// http://stackoverflow.com/questions/2300285/how-do-i-escape-in-sql-server
	$search  = str_replace( '_', '\_', $search );
	$_search = str_replace( '_', '\_', $_search );

	// search
	// results show faster query times using 2 LIKE vs 2 wildcards
	$rows = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT * 
		FROM $wpdb->options 
		WHERE option_name LIKE %s 
		OR option_name LIKE %s",
			$search,
			$_search
		),
		ARRAY_A
	);

	// loop
	if ( $rows ) {
		foreach ( $rows as $row ) {

			/*
			Use regex to find "(_)taxonomy_(term_id)_(field_name)" and populate $matches:
			Array
			(
			[0] => _category_3_color
			[1] => _
			[2] => 3
			[3] => color
			)
			*/
			if ( ! preg_match( "/^(_?){$taxonomy}_(\d+)_(.+)/", $row['option_name'], $matches ) ) {
				continue;
			}

			// vars
			$term_id    = $matches[2];
			$meta_key   = $matches[1] . $matches[3];
			$meta_value = $row['option_value'];

			// update
			// memory usage reduced by 50% by using a manual insert vs update_metadata() function.
			// update_metadata( 'term', $term_id, $meta_name, $meta_value );
			$wpdb->insert(
				$wpdb->termmeta,
				array(
					'term_id'    => $term_id,
					'meta_key'   => $meta_key,
					'meta_value' => $meta_value,
				)
			);

			// log
			acf_dev_log( 'ACF Upgrade 5.5.0 Term.', $term_id, $meta_key );

			// action
			do_action( 'acf/upgrade_550_taxonomy_term', $term_id );
		}
	}

	// action for 3rd party
	do_action( 'acf/upgrade_550_taxonomy', $taxonomy );
}