Home Forums General Issues Link user to post but how to get post linked to user


Link user to post but how to get post linked to user

  • Hi all ๐Ÿ™‚ !

    So the question is perhaps stupid but I’m easily able to link a user to a post but unable to get the post linked to a user :(…
    I’m using an ACF relationship field “user” and when I’m writing a post I just have to choose a unique user in the list of users that I want to link to that article.
    Now, when I’m on custom profile page of that user I want to show the title and the permalink to the post I linked to that user.

    Please, someone to help me with that (I’m sure) pretty simple problem?




  • Hi, Not sure how your linking your posts so I am going to go on a limb and just give you some code you might be able to edit to get your data but note I could be way of due to not seeing your codes or having much to go on.

    I have not tested this so I might not have it all completely correct you will need to debug if not or change to suit your needs.

    // only fires when viewing currently logged-in user's profile page
    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    // only fires when viewing another user's profile page
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    // you can add different actions for either of the above hocks
    //adding our function for calling on user profile both edit and show
    function my_show_extra_profile_fields($user_id) { 
    // get the users name as user field in ACF
    $user_d = get_user_by( 'id', $user_id )
    $nice_name_user = $user_d->username;
    //query for info of posts change to suit below
        'orderby' => 'post_date' , 
        'post_type' => 'post or custom post name' , 
        'meta_key' => 'custom acf name', 
        'meta_value' => $nice_name_user ,
        'posts_per_page' => 100)
    if (have_posts()) :
    while (have_posts()) : the_post();
    //echo out your info here
    echo'<p>'.get_field( "the acf name of filed to display from the post" ).'</p>';
  • Hi skdsam ๐Ÿ™‚ !

    Thanks a lot !! I modify a little the code but I get what I want :).
    And even if I didin’t give you a lot of elements, you have gone in the good limb :D.

    Here is the code I use from yours:

    $args =array(
    	'orderby' => 'post_date' , 
    	'post_type' => 'post' , 
    	'meta_key' => 'utilisateur_lie', 
    	'meta_value' => $profil_ID,
    	'posts_per_page' => 1
    	$query = new WP_Query($args);
    	if ($query->have_posts()) :
    	while ($query->have_posts()) : $query->the_post(); update_post_caches($posts);
    		echo'<p><b>Dรฉcouvrir son histoire :</b> <a href="'.get_permalink().'">'.get_the_title().'</a></p>';

    And here is some more informations:
    On my website, In my fields groups I’ve created a field group “User infos” where I’ve added two custom fields (a ‘Select: “shop”, and a ‘Select’:”town”).
    They “appear” when I add or edit users profiles.

    Then I’ve created an other fields group : “User post” where I have added a field “User” (named ‘utilisateur_lie’) who “appears” when I create/edit a post in a specific category (“Stories”) to link a post to a user.
    So now favor to your code I’m able to display the story belonging to that user :).
    That’s really great !

    Now, can I ask you something more complex?
    Will it be possible to retrieve and display a list of posts “linked” to a shop (for which several users work). A kind of grouping post by shop.

    I will really be very grateful if you could help me with this problem ;).

    Anyway, thanks a lot for your time and your help skdsam ๐Ÿ˜€ !!

    Have a great day ๐Ÿ™‚ !!



Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Link user to post but how to get post linked to user’ is closed to new replies.