cp_add_object_relationship( int $left_object_id, string $left_object_type, string $right_object_type, int $right_object_id )

Create a relationship between two recognized objects.


Description

Objects must be among the array produced by cp_recognized_relationship_objects().


Parameters

$left_object_id

(Required) ID of first object.

$left_object_type

(Required) Name of type of first object.

$right_object_type

(Required) Name of type of second object.

$right_object_id

(Required) ID of second object.


Return

(int) $relationship_id Relationship ID of a relationship if it already exists.<br> Otherwise relationship ID of newly-created relationship.


Source

File: wp-includes/object-relationships.php

function cp_add_object_relationship( $left_object_id, $left_object_type, $right_object_type, $right_object_id ) {

	// Check if relationship already exists: if so return relationship ID.
	$relationship_id = cp_object_relationship_exists( $left_object_id, $left_object_type, $right_object_type, $right_object_id );

	if ( absint( $relationship_id ) === 0 ) {

		// Relationship does not exist, so insert it now.
		global $wpdb;
		$table_name = $wpdb->prefix . 'object_relationships';

		$relationship_array = array(
			'left_object_id'    => $left_object_id,
			'left_object_type'  => $left_object_type,
			'right_object_type' => $right_object_type,
			'right_object_id'   => $right_object_id,
		);

		// $wpdb->insert sanitizes data.
		$added = $wpdb->insert( $table_name, $relationship_array );
		$relationship_id = $wpdb->insert_id;

		// Hook after relationship added.
		do_action( 'added_object_relationship', $relationship_id, $left_object_id, $left_object_type, $right_object_type, $right_object_id );
	}

	// Return relationship ID of newly-inserted relationship.
	return $relationship_id;
}


Changelog

Changelog
Version Description
CP-2.2.0 Introduced. CP-2.2.0