cp_get_object_relationship_ids( int $left_object_id, string $left_object_type, string $right_object_type )
Get IDs of all the recognized objects related to the known object.
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.
Return
(array|WP_Error) $target_ids ID of each object with which the first object has a relationship.<br> WP_Error when a param of an incorrect type is specified.
Source
File: wp-includes/object-relationships.php
function cp_get_object_relationship_ids( $left_object_id, $left_object_type, $right_object_type ) {
// Error if $left_object_id is not a positive integer
if ( filter_var( $left_object_id, FILTER_VALIDATE_INT ) === false ) {
$item = 'string';
if ( $left_object_id === 0 ) {
$item = 0;
} elseif ( is_object( $left_object_id ) ) {
$item = 'object';
} elseif ( is_array( $left_object_id ) ) {
$item = 'array';
}
/* translators: %s: String, 0, object, or array. */
$message = sprintf( __( 'cp_add_object_relationship() expects parameter 1 to a positive integer; %s given.' ), $item );
return new WP_Error( 'left_object_id', $message );
}
// Get array, and create list, of recognized relationship objects.
$recognized_relationship_objects = cp_recognized_relationship_objects();
$object_list = implode( ', ', $recognized_relationship_objects );
// Error if $left_object_type is not a non-null string of an appropriate value.
if ( ! in_array( $left_object_type, $recognized_relationship_objects ) ) {
$item = $left_object_type;
if ( is_int( $left_object_type ) ) {
$item = 'integer';
} elseif ( is_object( $left_object_type ) ) {
$item = 'object';
} elseif ( is_array( $left_object_type ) ) {
$item = 'array';
}
/* translators: 1: List of recognized relationship objects, 2: Integer, object, or array. */
$message = sprintf( __( 'cp_add_object_relationship() expects parameter 2 to be one of %s; %s given.' ), $object_list, $item );
return new WP_Error( 'left_object_type', $message );
}
// Error if $right_object_type is not a non-null string of an appropriate value
if ( ! in_array( $right_object_type, $recognized_relationship_objects ) ) {
$item = $right_object_type;
if ( is_int( $right_object_type ) ) {
$item = 'integer';
} elseif ( is_object( $right_object_type ) ) {
$item = 'object';
} elseif ( is_array( $right_object_type ) ) {
$item = 'array';
}
/* translators: 1: List of recognized relationship objects, 2: Integer, object, or array. */
$message = sprintf( __( 'cp_add_object_relationship() expects parameter 3 to be one of %s; %s given.' ), $object_list, $item );
return new WP_Error( 'left_object_type', $message );
}
// Good to go, so query database table.
global $wpdb;
$table_name = $wpdb->prefix . 'object_relationships';
// Query database table from left to right.
$sql1 = $wpdb->prepare( "SELECT right_object_id FROM $table_name WHERE left_object_id = %d AND left_object_type = %s AND right_object_type = %s", $left_object_id, $left_object_type, $right_object_type );
// Query database table from right to left.
$sql2 = $wpdb->prepare( "SELECT left_object_id FROM $table_name WHERE right_object_id = %d AND right_object_type = %s AND left_object_type = %s", $left_object_id, $left_object_type, $right_object_type );
// Results are in the form of two objects.
$rows1 = $wpdb->get_results( $sql1 );
$rows2 = $wpdb->get_results( $sql2 );
// Create array of target object IDs, starting with an empty array.
$target_ids = array();
if ( ! empty( $rows1 ) ) {
foreach ( $rows1 as $row ) {
$target_ids[] = (int) $row->right_object_id; // cast each one as an integer
}
}
if ( ! empty( $rows2 ) ) {
foreach ( $rows2 as $row ) {
$target_ids[] = (int) $row->left_object_id; // cast each one as an integer
}
}
// Return the array of integers or an empty array.
return $target_ids;
}
Changelog
Version | Description |
---|---|
CP-2.2.0 | Introduced. CP-2.2.0 |