Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active March 15, 2025 20:55
Show Gist options
  • Save Crocoblock/102314ceabe4e6759e1ff196f1d91079 to your computer and use it in GitHub Desktop.
Save Crocoblock/102314ceabe4e6759e1ff196f1d91079 to your computer and use it in GitHub Desktop.
JetEngine Relations: update items programmatically, register a relation programmatically
<?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;
} );
@aayla-secura
Copy link

aayla-secura commented Oct 15, 2024

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:

$rel_props = [
	'name' => 'Parent to child post',
	'parent_object' => 'posts::parent',
	'child_object' => 'posts::child',
	'type' => 'one_to_many',
	'child_control' => false,
	'parent_control' => true,
	'labels' => [
		'name' => 'Parent to child post',
		'parent_page_control_title' => 'Connected child posts',
		'parent_page_control_connect' => 'Connect a child post',
		'parent_page_control_select' => 'Select a child post',
	],
	'meta_fields' => [],
	'rest_post_access' => 'manage_options'
];
jet_engine()->relations->data->set_request($rel_props);
$rel_id = jet_engine()->relations->data->create_item(false);

// If you need to use the new relation immediately, you need to instantiate it
// as it won't be loaded into the manager until the next request
if (! class_exists('\Jet_Engine\Relations\Relation')) {
	// JetEngine won't load the class file if there are no relations defined
	require_once jet_engine()->relations->component_path('relation.php');
}

$relation = new \Jet_Engine\Relations\Relation($rel_id, $rel_props);
jet_engine()->relations->add_active_relation($rel_id, $relation);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment