Author meta just showing numbers
Support › MB Relationships › Author meta just showing numbers
- This topic has 11 replies, 2 voices, and was last updated 3 years, 1 month ago by
Long Nguyen.
-
AuthorPosts
-
February 12, 2022 at 7:35 AM #33857
Yasmine
ParticipantHello - 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.
February 12, 2022 at 3:51 PM #33871Long Nguyen
ModeratorHi,
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/#postsFebruary 14, 2022 at 7:09 PM #33895Yasmine
ParticipantThanks - 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!
February 14, 2022 at 7:20 PM #33896Yasmine
ParticipantFor example, would I write: get_the_title('university' $post) in the author meta option ?
February 15, 2022 at 4:51 AM #33900Yasmine
ParticipantHello - 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)
February 15, 2022 at 4:06 PM #33907Long Nguyen
ModeratorHi,
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
February 15, 2022 at 5:03 PM #33909Yasmine
ParticipantHi 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; }
February 15, 2022 at 6:36 PM #33910Yasmine
ParticipantBut 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', ], ], ] ); }
February 16, 2022 at 2:07 PM #33928Long Nguyen
ModeratorHi,
There are two cases:
- If you want to use the field
post
(user meta). You can use this code to show the post title - If you want to use the relationship. 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
- Remember to change 123 with the user ID.
February 18, 2022 at 12:53 AM #33966Yasmine
ParticipantThank 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.
February 18, 2022 at 12:53 AM #33967Yasmine
ParticipantThank 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.
February 19, 2022 at 5:23 PM #34005Long Nguyen
ModeratorHi,
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
- If you want to use the field
-
AuthorPosts
- You must be logged in to reply to this topic.