Support Forum
Support › MB Relationships › Autopopulating relationshipsResolved
Hi Long,
I need to autopopulate the relationship depending on the value of a post field. This is because when i upload the data, I can populate the user post field but I cannot populate the user relationship field. But I don't understand where I am going wrong with this code. My relationship is set from the post => User. And my aim is to assign the user to that post (university).
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
if ( 'academics_university' === $config['id'] ) { //'academics_university is the ID of a user meta field
$related_post_ids = empty( $_POST['university'] ) ? (array) $_post['academics_university'] : []; //'university' is the id of my CPT
foreach ( $related_post_ids as $related_post ) {
MB_Relationships_API::add( $post_id, $related_post_id, 'university_to_user' ); //this is the ID of the relationship university => user
}
}
}, 10, 2 );
What have I done wrong?
Also I think this is my last issue, then I promise I will leave you be, and no more questions.
Many thanks,
Yasmine
Hi Yasmine,
Can you please share the code that creates the meta box and custom fields added to the frontend form? And share the code that creates the relationship university_to_user
. I need to know which is post type submitted via the frontend form.
Regarding your code, you should check the field group ID (meta box ID) with the variable $config['id']
instead of the field ID. Some screenshots of your case would be better to understand.
Read more on the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions
This is the user frontend:
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Profile', 'your-text-domain' ),
'id' => 'academic_profile',
'type' => 'user',
'tabs' => [
'basic_info_tab' => [
'label' => 'Basic Info',
'icon' => 'dashicons-admin-users',
],
'academic_profile' => [
'label' => 'Academic Profile',
'icon' => 'dashicons-welcome-learn-more',
],
'social_sharing' => [
'label' => 'Social Sharing',
'icon' => 'dashicons-share1',
],
],
'fields' => [
[
'name' => __( 'Academic Title', 'your-text-domain' ),
'id' => $prefix . 'academic_title',
'type' => 'select_advanced',
'options' => [
'Dr' => __( 'Dr', 'your-text-domain' ),
'Professor' => __( 'Professor', 'your-text-domain' ),
],
'columns' => 3,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'First Name', 'your-text-domain' ),
'id' => $prefix . 'first_name',
'type' => 'text',
'columns' => 4,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Last Name', 'your-text-domain' ),
'id' => $prefix . 'last_name',
'type' => 'text',
'columns' => 5,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Pronouns', 'your-text-domain' ),
'id' => $prefix . 'user_pronouns',
'type' => 'text',
'datalist' => [
'id' => '62583f61cc701',
'options' => [
'He/Him
',
'She/Her
',
'They/Them',
],
],
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Nationality', 'your-text-domain' ),
'id' => $prefix . 'user_nationality',
'type' => 'select_advanced',
'options' => [
//deleted options to make shorter
],
'multiple' => true,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Biography', 'your-text-domain' ),
'id' => $prefix . 'description',
'type' => 'textarea',
'desc' => __( '1-2 sentences', 'your-text-domain' ),
'tooltip' => [
'icon' => 'info',
'position' => 'top',
'content' => 'Write a sentence to introduce yourself. Keep it as short as a tweet - 280 characters.',
],
'limit' => 280,
'textarea_rows' => 4,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Profile Picture', 'your-text-domain' ),
'id' => $prefix . 'custom_avatar',
'type' => 'single_image',
'force_delete' => true,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'University', 'your-text-domain' ),
'id' => $prefix . 'academics_university', /*this is the field that I am trying to duplicate into the relationship*/
'type' => 'post',
'post_type' => ['university'],
'field_type' => 'select_advanced',
'tab' => 'academic_profile',
],
[
'name' => __( 'Faculty', 'your-text-domain' ),
'id' => $prefix . 'faculty',
'type' => 'select_advanced',
'options' => [
= //deleted options to make shorter
],
'columns' => 6,
'tab' => 'academic_profile',
],
[
'name' => __( 'Academic Position', 'your-text-domain' ),
'id' => $prefix . 'academic_position',
'type' => 'select_advanced',
'options' => [
//deleted options to make shorter
],
'columns' => 6,
'tab' => 'academic_profile',
],
[
'name' => __( 'University Profile Page', 'your-text-domain' ),
'id' => $prefix . 'university_profile',
'type' => 'url',
'tab' => 'social_sharing',
],
[
'name' => __( 'Linkedin Link', 'your-text-domain' ),
'id' => $prefix . 'linkedin',
'type' => 'url',
'tab' => 'social_sharing',
],
[
'name' => __( 'Twitter Handle', 'your-text-domain' ),
'id' => $prefix . 'twitter',
'type' => 'text',
'prepend' => '@',
'attributes' => [
'pattern' => '[^()/><\\][\\\\\\x22,;|]+',
],
'tab' => 'social_sharing',
],
[
'name' => __( 'Orchid Profile', 'your-text-domain' ),
'id' => $prefix . 'orchid',
'type' => 'url',
'tab' => 'social_sharing',
],
[
'name' => __( 'Email', 'your-text-domain' ),
'id' => $prefix . 'user_email',
'type' => 'email',
'tab' => 'social_sharing',
],
[
'name' => __( 'Email Sharing', 'your-text-domain' ),
'id' => $prefix . 'email_sharing',
'type' => 'checkbox',
'label_description' => __( 'Include your email on your profile so members can get in touch', 'your-text-domain' ),
'std' => true,
'tab' => 'social_sharing',
],
],
];
return $meta_boxes;
}'
University => User relationship:
<?php
add_action( 'mb_relationships_init', 'your_prefix_function_name' );
function your_prefix_function_name() {
MB_Relationships_API::register( [
'id' => 'university_to_user',
'from' => 'university',
'to' => [
'object_type' => 'user',
'post_type' => 'post',
'taxonomy' => 'category',
],
] );
}
Does this help at all?
Ok so do you mean do this instead:
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
if ( 'academic_profile' === $config['id'] ) { // now field group ID
$related_post_ids = empty( $_POST['university'] ) ? (array) $_post['academics_university'] : [];
foreach ( $related_post_ids as $related_post ) {
MB_Relationships_API::add( $post_id, $related_post_id, 'university_to_user' );
}
}
}, 10, 2 );
Hi,
It does not mean when you use the field group (meta box) ID, which is assigned to the user as user meta, in the frontend submission form. This meta box ID should be used in the user shortcodes like user profile, user registration ... You should use a field group ID assigned to the post type university
to add to the frontend submission shortcode. Please read more here
https://docs.metabox.io/extensions/mb-user-profile/#edit-profile-form
https://docs.metabox.io/extensions/mb-frontend-submission/#advanced-form
'title' => __( 'Profile', 'your-text-domain' ),
'id' => 'academic_profile',
'type' => 'user', //here
If you want to set the connection between a user to a post selected (user meta) when editing the user profile, please use the action rwmb_profile_after_save_user
https://docs.metabox.io/extensions/mb-user-profile/#user-actions
Hi Long,
I am really sorry but I do not understand what you mean. Do you mean make it like this:
add_action( 'rwmb_profile_after_save_user', function( $config, $user_id ) {
if ( 'academic_profile' === $config['id'] ) {
$related_post_ids = empty( $_POST['university'] ) ? (array) $_post['academics_university'] : [];
foreach ( $related_post_ids as $related_post ) {
MB_Relationships_API::add( $user_id, $related_post_id, 'university_to_user' );
}
}
}, 10, 2 );
I would be happy to use the normal relationship
field if it could be ordered within the frontend form - but instead it stands alone- I made a Loom to show you. So I want to use the post
fields UX as a workaround to select a post, which value also equals a relationship - so it can be hidden from the frontend form
Ok - I will make the frontend relationship
work, and will redesign the form - maybe break down into three meta groups. I think you're right and its the easiest thing to do.
Which means, I would just need to output the user from relationship
. I tried the shortcode in the documentation, but it wasn't working for me (tried to
and from
directions to be sure).
So Im now trying to add my own shortcode:
add_shortcode( 'show_user_post_related', function() {
$author_id = get_post_field( 'post_author', get_queried_object_id() );
$post_id = rwmb_meta( 'university_2_user_relationships_from', ['object_type' => 'user'], $author_id );
if ( empty( $post_id ) ) { // where am I going wrong here?
return '';
}
$output = get_the_title( $post_id );
return $output;
} );
But what have I done wrong? If I can fix this shortcode, then I will (finally) give up on the auto-populating idea!
Hi Yasmine,
Can you please let me know which form do you use to edit data on the frontend? Is that the form for edit user profile or edit/submit posts? Because depending on each type of the form, the action is different.
If possible, please paste the shortcode of the form here, so we understand it better.
Hi Anh,
Thank you for the reply - it is the user profile I am using. And then I wish to show the user info on an about the author section in a post.
Here is the short code:
[mb_user_profile_info id= "academic_profile, university_2_user_relationships_from"
label_submit="Continue" confirmation="Academic details were updated"]
Hi Yasmine,
If users create/submit a post, WordPress automatically add them as the author of the post (university). So if you want to show the universities that belong to an author, I think no need to create a relationship between user and university, just create a query with the author parameter.
Please read more on this documentation https://developer.wordpress.org/reference/classes/wp_query/#author-parameters
Sorry, no the authors are not the author of the university post, this is just associated with them. So this does not work. I am trying to display this information within a different custom post type (research). And a specific field within the university post (university_Name). Please can you tell me how to adapt my shortcode so it works!
Should it be something more like:
add_shortcode( 'show_user_post_related', function() {
$author_id = get_post_field( 'post_author', get_queried_object_id() );
$post_id = rwmb_meta( 'university_group', ['object_type' => 'university'], $author_id ); //university_group is my name of custom field group, object_type is my name of cpt
if ( empty( $post_id ) ) {
return '';
}
$output = get_the_title( $post_id );
return $output;
} );
The field within the university I want to fetch is also the post_title
Hi Yasmine,
To output the connected posts to a user, please follow this documentation https://docs.metabox.io/extensions/mb-relationships/#posts
Here is the example code:
add_shortcode( 'show_user_post_related', function() {
$author_id = get_post_field( 'post_author', get_queried_object_id() );
if ( empty( $author_id ) ) {
return 'No author ID';
}
$connected = new WP_Query( [
'post_type' => 'university',
'relationship' => [
'id' => 'university_to_user',
'to' => $author_id, // You can pass object ID or full object
],
'nopaging' => true,
] );
$output = '';
while ( $connected->have_posts() ) : $connected->the_post();
$output .= the_title();
endwhile;
wp_reset_postdata();
return $output;
} );
THANK YOU - it works! I can use this for every other relationship i'll need to reference.