Is it Possible to Have Relationship Metabox Editable Only on One Side?
Support › MB Relationships › Is it Possible to Have Relationship Metabox Editable Only on One Side?
- This topic has 7 replies, 2 voices, and was last updated 3 years, 1 month ago by
Long Nguyen.
-
AuthorPosts
-
March 19, 2022 at 2:34 AM #34602
[email protected]
ParticipantHello,
Is it possible to have a relationship field editable only on one side of the relation? Lets say I have a list of Courses and the list of Applications. I want to select one Course for each Application and I want to see the list of the Applications in the detail of the Course. But I don't want to have a possibility to change anything in the list of the Applications inside the Course detail.
So far, I was able to limit the number of Courses selectable in the Application detail, but I'm not able to disable editing of the Applications in the Course detail.
Is it possible, for example using some class?
Thanks
PetrMarch 21, 2022 at 6:04 AM #34687Long Nguyen
ModeratorHi,
Yes, it is possible. You can use the setting
reciprocal
when registering the relationship. If you set reciprocal for different object types (CPT) like your case, the connection meta box only shows on the from object type page.Refer to the documentation https://docs.metabox.io/extensions/mb-relationships/#reciprocal-relationships
March 21, 2022 at 6:57 AM #34689[email protected]
ParticipantHi Long,
I must be very bad at explaining my questions... I clearly stated, that I want to show the metabox in both CPTs. I only want one of them to be disabled for editing. Again, I want to SEE them both, but I don't want to EDIT one of them.
Thanks again
PetrMarch 21, 2022 at 11:04 PM #35182Long Nguyen
ModeratorHi,
You can use the field type
select
and add the attributedisabled
to an object type to disable edit/add more connected objects. For example:MB_Relationships_API::register( [ 'id' => 'job-to-post', 'from' => [ 'object_type' => 'post', 'post_type' => 'job', 'field' => [ 'type' => 'select', 'disabled' => true //here ] ], 'to' => [ 'object_type' => 'post', 'post_type' => 'post', ], ] );
Read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#syntax
https://docs.metabox.io/field-settings/March 22, 2022 at 1:43 AM #35185[email protected]
ParticipantHello Long,
Thank you, I'll will try it, but first I have to say, that I'm usig GUI for creating the relationship. Since there's (probably) not a field for this parameter in there, how should I update my relationship settings? Or do I have to register this relationship only by code?
Thanks
PetrMarch 22, 2022 at 9:37 PM #35206Long Nguyen
ModeratorHi,
The builder does not support options to customize the field settings. You need to use the code to get more control over field settings.
March 22, 2022 at 10:26 PM #35213[email protected]
ParticipantHi Long,
OK, I will use the code, but HOW should I do this? Do I need to remove the orginal relationship from the GUI and start from the scratch or can I use some code to change only this option separately?
Thanks
PetrMarch 23, 2022 at 12:57 PM #35231Long Nguyen
ModeratorHi,
You can export the relationship from the builder to get the PHP code, move it to the trash or delete it. Then add the code to the file functions.php in the theme/child theme folder.
The data is already saved into the database so don't worry it will be lost after deleting the relationship from the builder.
Refer to this documentation https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code
-
AuthorPosts
- You must be logged in to reply to this topic.