Support Forum
Support › MB Relationships › Author meta just showing numbers
Hello - I am trying to show the related post for the user (author). For example, my author is linked to a university CPT. I can add the university from their user profile. But when I try to use the author meta and add the custom field ID, it just shows numbers. And I can't work out how to make it show the linked post title instead! My aim is to show the linked post title, and so the reader can click on this and then read about the university.
Hi,
If it returns the post ID (number), you can use WordPress functions to get the post title and permalink
https://developer.wordpress.org/reference/functions/get_the_title/
https://developer.wordpress.org/reference/functions/get_the_permalink/
Or follow this documentation to use the WP Query to show related posts with title and link
https://docs.metabox.io/extensions/mb-relationships/#posts
Thanks - and do I add into the php code - or do I need to include into the shortcode on my template. Sorry - not sure where to add it!
For example, would I write: get_the_title('university' $post) in the author meta option ?
Hello - I just wanted to elaborate a little further. As it is still not working, and I thought it would be easier to give a fuller picture:
'<div>get_the_title( $university );
$academics_university = rwmb_meta( 'academics_university' ); //maybe do I have to write its a custom field here?
$academics_university = get_the_title( $university )</div>'
To explain the above: $university is my custom post type. $academics_university is a custom relational post field saved to the user. I created one relationship between user and university too. It works on the backend.
But I am trying to get the $university to show next to the author of a different cpt ($research). The author creates these posts and these posts are also connected to the user and to the university cpt. I believe I need to use the author meta option (on elementor pro template) with the meta key 'academics_university' - as the related post is saved to the user.
Right now, the post ID shows. But I cannot get the university to show and link to the selected $university.
I would really appreciate your advice how to do this! I am not a developer in any way (you can probably tell)
Hi,
Can you please share the code that creates the relationship between the user and CPT university? If you are using MB Builder, please follow this documentation to get PHP code https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code
Hi Long, yes of course (I deleted the other custom fields in group as was too long for this message to send)
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Academic: Profile', 'your-text-domain' ),
'id' => 'academic_profile_group',
'type' => 'user',
'fields' => [
[
'name' => __( 'University', 'your-text-domain' ),
'id' => $prefix . 'academics_university',
'type' => 'post',
'post_type' => ['university'],
'field_type' => 'select_advanced',
'required' => true,
'admin_columns' => [
'position' => 'after',
'sort' => true,
'searchable' => true,
'filterable' => true,
],
'columns' => 1,
],
return $meta_boxes;
}
But the above is from the post field on the custom fields form. I also created a relationship between the user and university. And was actually not sure if this part was needed or not and how to tie it in:
<?php
add_action( 'mb_relationships_init', 'your_prefix_function_name' );
function your_prefix_function_name() {
MB_Relationships_API::register( [
'id' => 'relationship_user_university',
'reciprocal' => true,
'from' => [
'object_type' => 'user',
'post_type' => 'post',
'taxonomy' => 'category',
'meta_box' => [
'title' => 'user_to_university_relationship',
],
'field' => [
'name' => 'User to University Relationship',
],
],
'to' => [
'object_type' => 'post',
'post_type' => 'university',
'meta_box' => [
'title' => 'university_to_user_relationship',
],
'field' => [
'name' => 'University to User Relationship',
],
],
] );
}
Hi,
There are two cases:
post
(user meta). You can use this code to show the post title$post_id = rwmb_meta( 'academics_university', ['object_type' => 'user'], 123);
echo get_the_title( $post_id );
Refer to the documentation https://docs.metabox.io/fields/post/#template-usage
https://docs.metabox.io/extensions/mb-user-meta/#getting-field-value
$connected = new WP_Query( [
'relationship' => [
'id' => 'relationship_user_university',
'from' => 123,
],
'nopaging' => true,
] );
while ( $connected->have_posts() ) : $connected->the_post();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
wp_reset_postdata();
Refer to the documentation https://docs.metabox.io/extensions/mb-relationships/#posts
Thank you Long for the reply!
I still can't get it working...
I tried:
{
$post_author_id = get_post_field( 'post_author', $post_id );
}
$post_id = rwmb_meta( 'academics_university', ['object_type' => 'user'], $post_author_id );
echo get_the_title( $post_id )
?>
So I guess I don't know what to add in replacement to your "123" to make it work for dynamic post author. Can you advise?
Secondly, I don't really understand when I would use field post user meta versus the relationship. How do they differ - and could you possibly provide an example of how they differ?
It is likely that I will want to use additional fields from the related post to show in future $research templates
Thank you - I appreciate it.
Thank you Long for the reply! But I still can't get it working...
I tried:
{
$post_author_id = get_post_field( 'post_author', $post_id );
}
$post_id = rwmb_meta( 'academics_university', ['object_type' => 'user'], $post_author_id );
echo get_the_title( $post_id )
?>
So I guess I don't know what to add in replacement to your "123" to make it work for dynamic post author. Can you advise?
Secondly, I don't really understand when I would use field post user meta versus the relationship. How do they differ - and could you possibly provide an example of how they differ?
It is likely that I will want to use additional fields from the related post to show in future $research templates
Thank you - I appreciate it.
Hi,
If you want to output the user meta in Elementor page builder, you can try to create a shortcode then add the shortcode to the builder. For example:
// Requires PHP 7+.
add_shortcode( 'show_user_post_related', function() {
$author_id = get_post_field( 'post_author', get_queried_object_id() );
$post_id = rwmb_meta( 'academics_university', ['object_type' => 'user'], $author_id );
if ( empty( $post_id ) ) {
return '';
}
$output = get_the_title( $post_id );
return $output;
} );
// Usage: put [show_user_post_related] into your post content or page builder modules.
Screenshot https://monosnap.com/file/VjRHbqnqdJOUcCg5DH62G7mImc9Dep