BP_XProfile_Field::update_position( int $field_id, int|null $position = null, int|null $field_group_id = null )

Update field position and/or field group when relocating.

Description

Parameters

$field_id

(int) (Required) ID of the field to update.

$position

(int|null) (Optional) Field position to update.

Default value: null

$field_group_id

(int|null) (Optional) ID of the field group.

Default value: null

Return

(boolean)

Source

File: bp-xprofile/classes/class-bp-xprofile-field.php

	public static function update_position( $field_id, $position = null, $field_group_id = null ) {
		global $wpdb;

		// Bail if invalid position or field group.
		if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) {
			return false;
		}

		// Get table name and field parent.
		$table_name = buddypress()->profile->table_name_fields;
		$sql        = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id );
		$parent     = $wpdb->query( $sql );

		// Update $field_id with new $position and $field_group_id.
		if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {

			// Update any children of this $field_id.
			$sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id );
			$wpdb->query( $sql );

			// Invalidate profile field cache.
			wp_cache_delete( $field_id, 'bp_xprofile_fields' );

			do_action( 'xprofile_updated_field_position', $field_id, $position, $field_group_id );

			return $parent;
		}

		return false;
	}

Changelog

Changelog
Version Description
BuddyPress 1.5.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.