WPSEO_Plugin_Importer::meta_key_clone()protectedYoast 1.0

Helper function to clone meta keys and (optionally) change their values in bulk.

Метод класса: WPSEO_Plugin_Importer{}

Хуков нет.


true|false. Clone status.


// protected - в коде основоного (родительского) или дочернего класса
$result = $this->meta_key_clone( $old_key, $new_key, $replace_values );
$old_key(строка) (обязательный)
The existing meta key.
$new_key(строка) (обязательный)
The new meta key.
An array, keys old value, values new values.
По умолчанию: []

Код WPSEO_Plugin_Importer::meta_key_clone() Yoast 22.4

protected function meta_key_clone( $old_key, $new_key, $replace_values = [] ) {
	global $wpdb;

	// First we create a temp table with all the values for meta_key.
	$result = $wpdb->query(
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- This is intentional + temporary.
			"CREATE TEMPORARY TABLE tmp_meta_table SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s",
	if ( $result === false ) {
		return false;

	// Delete all the values in our temp table for posts that already have data for $new_key.
			"DELETE FROM tmp_meta_table WHERE post_id IN ( SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s )",
			WPSEO_Meta::$meta_prefix . $new_key

	 * We set meta_id to NULL so on re-insert into the postmeta table, MYSQL can set
	 * new meta_id's and we don't get duplicates.
	$wpdb->query( 'UPDATE tmp_meta_table SET meta_id = NULL' );

	// Now we rename the meta_key.
			'UPDATE tmp_meta_table SET meta_key = %s',
			WPSEO_Meta::$meta_prefix . $new_key

	$this->meta_key_clone_replace( $replace_values );

	// With everything done, we insert all our newly cloned lines into the postmeta table.
	$wpdb->query( "INSERT INTO {$wpdb->postmeta} SELECT * FROM tmp_meta_table" );

	// Now we drop our temporary table.
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- This is intentional + a temporary table.
	$wpdb->query( 'DROP TEMPORARY TABLE IF EXISTS tmp_meta_table' );

	return true;