All these are to be called on 'init' action with the priority of 12 or later
Before init 12 there are no relations registered
This is to get Relation object by its ID (https://prnt.sc/zCOroTie86bL)
(object of this class \jet-engine\includes\components\relations\relation.php)
Necessary to work with relation
$relation = jet_engine()->relations->get_active_relations( $rel_id );
These are to get parents/children of the Post/CCT/Term/User from above-mentioned relation object
$object_id - ID of the object for which you need to get parents/children;
$field - 'ids' to return array of related items IDs, 'all' to return array of rows from database
$field = 'ids';
$related_ids = $relation->get_parents( $object_id, $field );
$related_ids = $relation->get_children( $object_id, $field );
Returns related items for given object. Automatically detects - we need to get children or parent items by object.
pass object as a first argument, not object id
$related_ids = $relation->get_related_items_for_object( $object );
Set relation update context - 'parent' or 'child'
$relation->set_update_context( 'parent' );
Update relation
$relation->update( $parent_object_id, $child_object_id );
if $parent_object_id is empty - delete all rows for current relations which have $child_object_id
if $child_object_id is empty - delete all rows for current relations which have $parent_object_id
CAUTION: if both empty - all rows for the relation will be deleted
$clear_meta - true / false, whether to delete associated meta; default true
$relation->delete_rows( $parent_object_id, $child_object_id, $clear_meta )
$relation->get_meta( $parent_object_id, $child_object_id, $meta_key );
$relation->update_meta( $parent_object_id, $child_object_id, $meta_key, $meta_value );
$relation->delete_meta( $parent_object_id, $child_object_id, $meta_key );
add_action( 'jet-engine/relation/update/before', function( $parent_object_id, $child_object_id, $relation ) {
}, 0, 3 );
runs before connecting items with relation
$parent_object_id, $child_object_id - IDs of items
$relation - object of Relation class
add_action( 'jet-engine/relation/update/after', function( $parent_object_id, $child_object_id, $item_id, $relation ) {
}, 0, 4 );
runs after connecting items with relation
$parent_object_id, $child_object_id - IDs of items
$item_id - ID of inserted record
$relation - object of Relation class
Very helpful, i was building a CRM in which the client need to upload leads from excel and assign it directly to agents. The assignment is relation but not to make agent as post author; thus this 3 lines of codes help a lot.
Thanks