Last active
May 28, 2025 16:46
-
-
Save Crocoblock/102314ceabe4e6759e1ff196f1d91079 to your computer and use it in GitHub Desktop.
JetEngine Relations: update items programmatically, register a relation programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'jet-engine/relations/raw-relations', function( $relations ) { | |
| $relations[999] = array( | |
| 'name' => 'User to post', | |
| 'parent_rel' => null, | |
| 'type' => 'many_to_many', | |
| 'is_legacy' => false, | |
| 'id' => 999, | |
| 'parent_object' => 'mix::users', | |
| 'child_object' => 'posts::post', | |
| 'parent_control' => true, | |
| 'child_control' => true, | |
| 'parent_manager' => true, | |
| 'child_manager' => true, | |
| 'parent_allow_delete' => true, | |
| 'child_allow_delete' => true, | |
| 'rest_get_enabled' => true, | |
| 'rest_post_enabled' => true, | |
| 'rest_get_access' => 'manage_options', | |
| 'rest_post_access' => 'manage_options', | |
| 'labels' => array ( | |
| 'name' => 'User to post', | |
| 'parent_page_control_title' => 'Posts, connected to the user', | |
| 'parent_page_control_connect' => 'Connect posts', | |
| 'parent_page_control_select' => 'Select posts', | |
| 'child_page_control_title' => 'Users, connected to the post', | |
| 'child_page_control_connect' => 'Connect users', | |
| 'child_page_control_select' => 'Select users', | |
| 'child_page_control_create' => 'Create user', | |
| 'parent_page_control_create' => 'Create post', | |
| ), | |
| ); | |
| return $relations; | |
| } ); | |
| //relation with meta fields and CCT-specific controls | |
| add_filter( 'jet-engine/relations/raw-relations', function( $relations ) { | |
| $relations[1000] = array ( | |
| 'id' => '1000', | |
| 'db_table' => true, | |
| 'parent_control' => true, | |
| 'child_control' => true, | |
| 'parent_manager' => true, | |
| 'child_manager' => true, | |
| 'parent_allow_delete' => true, | |
| 'child_allow_delete' => true, | |
| 'is_legacy' => false, | |
| 'rest_get_enabled' => true, | |
| 'rest_post_enabled' => true, | |
| 'name' => '', | |
| 'parent_object' => 'cct::blum_cct', | |
| 'child_object' => 'posts::blum-cpt', | |
| 'parent_rel' => NULL, | |
| 'type' => 'many_to_many', | |
| 'legacy_id' => '', | |
| 'rest_get_access' => 'manage_options', | |
| 'rest_post_access' => 'manage_options', | |
| 'cct' => | |
| array ( | |
| 'cct::blum_cct' => | |
| array ( | |
| 'title_field' => 'f1', | |
| 'create_fields' => | |
| array ( | |
| 'f2', | |
| 'rep_test', | |
| 'num3', | |
| 'num1', | |
| ), | |
| ), | |
| ), | |
| 'labels' => array ( | |
| 'name' => 'cct to cpt', | |
| 'parent_page_control_title' => 'Parent Object: label of relation box', | |
| 'parent_page_control_connect' => 'Parent Object: label of connect button', | |
| 'parent_page_control_select' => 'Parent Object: label of select item control', | |
| 'parent_page_control_create' => 'Parent Object: label of create button', | |
| 'child_page_control_title' => 'Child Object: label of relation box', | |
| 'child_page_control_connect' => 'Child Object: label of connect button', | |
| 'child_page_control_select' => 'Child Object: label of select item control', | |
| 'child_page_control_create' => 'Child Object: label of create button', | |
| ), | |
| 'meta_fields' => | |
| array ( | |
| array ( | |
| 'title' => 't1', | |
| 'name' => 't1', | |
| 'object_type' => 'field', | |
| 'width' => '100%', | |
| 'options' => array (), | |
| 'type' => 'text', | |
| ), | |
| array ( | |
| 'title' => 't2', | |
| 'name' => 't2', | |
| 'object_type' => 'field', | |
| 'width' => '100%', | |
| 'options' => array (), | |
| 'type' => 'text', | |
| ), | |
| ), | |
| ); | |
| return $relations; | |
| } ); |
if anyone is looking for a reference code to remove/delete relationships here it is
$relation_module = jet_engine()->relations;
if ( ! method_exists( $relation_module, 'get_active_relations' ) ) {
error_log(' Relation module not available.');
return;
}
$relation_id = 115;
$relation = $relation_module->get_active_relations( $relation_id );
if ( ! $relation ) {
error_log(' Relation ID 115 not found.');
return;
}
$project_id = 19359; // Replace with your actual project ID
$child_ids = [16931, 12895]; // Replace with your actual child booking IDs
foreach ( $child_ids as $child_id ) {
$relation->delete_rows( $project_id, $child_id, true );
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone who's wondering how to create the relationship permanently (i.e. saved in the database), rather than inserting via a hook, I thought it's worth posting this as an example: