Support Forum
Support › MB Relationships › Display Relationship value in GenerateBlocks dynamic data
Hi, I searched the forum and am a bit surprised I didn't find any topic on this.
I have created a relationship between 2 CPT: Events and Licensee.
I'm using GenerateBlocks (Pro) to display the list of events on a page, using the GB Query Loop (in case you're familiar).
I am able to retrieve custom fields created with Metabox, but I have no idea how to retrieve the related "Licensee" CPT for each Event.
Please before you tell me to go ask the GenerateBlocks folks, I'm actually more stuck at the Metabox level. I don't know how to display a relationship without code, and I don't know PHP :/ I have tried the shortcode, even outside of the GB Query Loop but on a CPT template, and it doesn't display anything. Looking at the HTML I don't even see any element for it.
So I know how to create a Relationship, but I don't know how to use it.
Is there any way to use relationships without writing PHP?
Hello Oliver,
Thanks for reaching out.
Regularly, on our documentation, you need to have a basic knowledge of coding to control how to output the value. If not, you can use the shortcode which you can find here https://docs.metabox.io/extensions/mb-relationships/#shortcode
If it does not work, please let me know how you register the relationship and what is the shortcode that you are using.
Regarding GenerateBlocks, I'm not sure if it is compatible with Meta Box to get the relationship. If there is a chance, you can see this article to use the Oxygen builder and display the relationship https://oxyhowto.com/more_hows/how-to-use-meta-box-bi-direction-relationship-in-oxygen-builder/
The integration is officially supported by Oxygen builder.
Thanks Peter.
Is there any way to output the relationship value into a custom field? That way I would be able to retrieve it with GenerateBlocks or any other tool, since custom fields are standard wordpress constructs.
As for the shortcode, I tried it but it doesn't work. Here is my shortcode:
[mb_relationships id="licensee-events" direction="from" mode=“ul”]
I constructed the Relationship via the UI, see here: https://i.imgur.com/jnCFX40.png
I created a page of the Event post type, I inserted the shortcode between 2 paragraphs so you can see, but it outputs nothing, it's not even in the DOM of the page: https://i.imgur.com/JDoHDdD.png
And in case you ask, yes I did select the Relationship during post creation.
Thank you
Also, I have a very basic understanding of coding and am often times able to modify code snippets for my needs. But I don't really know PHP. That said if there is a code snippet I can add via the plugin of the same name, I would be happy to try that.
Hello,
If you use the relationship shortcode on a page, not in the content of the post type event, please pass the ID of the post to the shortcode to the attribute items
. For example:
[mb_relationships id="licensee-events" items="12,34,56" direction="from" mode="ul"]
where 12,34,56
are post IDs of the post type event.
>> Is there any way to output the relationship value into a custom field?
You can try to add the shortcode to the field WYSIWYG and get the field value in the block/builder. But I think it is not necessary if you can add the shortcode to the page content or a block shortcode.
Thanks Peter. For some reason, today the shortcode is working when I insert it manually in the page. It's good to know, unfortunately this is not useful as I'm not able to retrieve it from outside the page (as with a custom field), and I can't rely on users to enter the shortcode manually anyway.
Relationships is one of the reason I purchased Metabox, but it seems like I won't be able to use it :/
Hello,
As I said before, you need to have a basic knowledge of PHP and WordPress coding to control the output data. Meta Box is not a tool with just a few clicks to set up anything on your site.
Hope that makes sense.
I understand. The way Relationships is implemented makes it hard to use because it's a different construct that WordPress or any other plugin don't know how to retrieve without custom API code.
I'd like to come back to the idea of outputting the relationship value in a custom field for any given post. You mentioned it may not be necessary because the shortcode is an option, but shortcodes are not useful in many dynamic situations. Is there any way to output the Relationship value in a (hidden) custom field?
It seems to me like it would be an easy solution to most issues - any other plugin able to work with custom fields would be able to retrieve it. The work of talking to the Relationships API would be done internally by Meta Box, but the rest of the WordPress install could simply refer to the relevant custom fields for the relationship value.
Hi Olivier, I had the same problem. GeneratePress Pro and GenerateBlocks Pro don't seem to be the best combination to deal with Meta Box relationships.
I wrote this code for one case where I wanted to add with the dynamic data option by GP and GB a 'person' related to a 'course' on the course single page (that person is in the course context a 'mentor'). This code generates a custom field (my_new_field_name) with a comma separated list of all mentors.
add_action( 'save_post_kurs', 'update_my_new_field_name', 10, 2 );
function update_my_new_field_name( $post_id, $post ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
$related_persons = MB_Relationships_API::get_connected([
'id' => 'kurs-person',
'from' => $post_id
]);
$mentor_names = [];
foreach ( $related_persons as $person ) {
$mentor_names[] = get_post_meta($person->ID, 'person_name', true);
}
update_post_meta( $post_id, 'my_new_field_name', implode( ', ', $mentor_names ) );
}
Hi Olivier, I tested the code a little but it seems that when a relationship changes this is not updated in the field. The code would need a specific Meta Box hook to allow to run when a relationship is updated. So, unfortuntately not a real dynamic solution.