Tagged: user to post relate
- This topic has 5 replies, 2 voices, and was last updated 4 years, 10 months ago by
friendlyfire3.
-
AuthorPosts
-
-
December 20, 2016 at 12:46 am #7678
friendlyfire3MemberI scoured the forums but couldn’t find anything definitive. Is it possible to relate a user to a post (without making them the author)?
If so, how is it done? I couldn’t find any tutorial in the docs but saw a few things on it like this: https://piklist.com/support/topic/post-to-user-relationships/
I tried :
<?php /* Title: Post Relate Post Type: */ piklist('field', array( 'type' => 'post-relate' ,'scope' => 'user' ,'template' => 'field' )); ?>but that doesn’t work. I also tried using user_meta in the scope but it’s not working. Any help is appreciated.
-
December 22, 2016 at 11:13 am #7687
SteveKeymasterAssuming this is in wp-admin, and when creating/editing a post. Try this (untested):
piklist('field', array( 'type' => 'group' ,'scope' => 'user' ,'label' => 'User' ,'relate' => array( 'scope' => 'post' ) ,'fields' => array( array( 'type' => 'text' ,'label' => 'Login' ,'field' => 'user_login' ,'columns' => 6 ) ) )); // Display related users $related = get_users(array( 'order' => 'DESC' ,'user_belongs' => $post->ID ,'user_relate' => 'post' )); ?> <?php if ($related): ?> <h4><?php _e('Related Users', 'piklist-demo');?></h4> <ol> <?php foreach ($related as $related_user): ?> <li><?php _e($related_user->user_login); ?></li> <?php endforeach; ?> </ol> <?php endif; ?> -
January 2, 2017 at 10:06 am #7708
friendlyfire3MemberThanks, Steve and happy new year!
This just seems to create an empty field on the view in wp-admin. I tried changing the type to checkbox but notta.
Any suggestions or do you have any learning guides on the relate system? I can only seem to find things in the forum and only in bits and pieces.
-
January 17, 2017 at 5:15 pm #7726
SteveKeymaster@friendlyfire3– you would enter the user login into that text field.
If you want to display a dropdown of users to choose from, this tutorial should help >
-
February 17, 2017 at 8:01 pm #7795
friendlyfire3MemberHey Steve, sorry to drudge up this post but took some time off and just getting back to this.
How would I go about getting a list of posts related to the user on the frontend?
I’ve tried this (some code excluded for brevity):
$user_id = get_current_user_id(); $key = '__relate_post'; $single = false; $user_posts = get_user_meta($user_id, $key, $single ); $related_proposals = get_posts(array( 'post_type' => 'proposal' // Set post type you are relating to. ,'posts_per_page' => -1 ,'post_belongs' => $user_id ,'post_status' => 'publish' ,'suppress_filters' => false // This must be set to false )); $output = ''; if (!empty($related_proposals) ) { $output .= '<ul>'; foreach($related_proposals as $related_proposal) { $output .= '<li><a href="'.get_post_permalink($related_proposal->ID).'">'.$related_proposal->post_title.'</a></li>'; } $output .= '</ul>'; } return $outputThe above returns absolutely nothing.
and then I tried this:
$user_id = get_current_user_id(); $key = '__relate_post'; $single = false; $user_posts = get_user_meta($user_id, $key, $single ); $related_proposals = get_posts(array( 'post_type' => 'proposal' // Set post type you are relating to. ,'posts_per_page' => -1 ,'post_belongs' => $user_posts ,'post_status' => 'publish' ,'suppress_filters' => false // This must be set to false )); $output = ''; if (!empty($related_proposals) ) { $output .= '<ul>'; foreach($related_proposals as $related_proposal) { $output .= '<li><a href="'.get_post_permalink($related_proposal->ID).'">'.$related_proposal->post_title.'</a></li>'; } $output .= '</ul>'; } return $outputThis returns all posts for the type- not just the ones associated.
And then tried this:
$user_id = get_current_user_id(); $key = '__relate_post'; $single = false; $user_posts = get_user_meta($user_id, $key, $single ); $related_proposals = get_posts(array( 'post_type' => 'proposal' // Set post type you are relating to. ,'posts_per_page' => -1 ,'include' => $user_posts ,'post_status' => 'publish' ,'suppress_filters' => false // This must be set to false )); $output = ''; if (!empty($related_proposals) ) { $output .= '<ul>'; foreach($related_proposals as $related_proposal) { $output .= '<li><a href="'.get_post_permalink($related_proposal->ID).'">'.$related_proposal->post_title.'</a></li>'; } $output .= '</ul>'; } return $outputand using “include” rather than post_belongs works but if there’s no ids in the array from the user meta, then all posts show up.
None of these do what I intend. I just want to show only the posts that we’ve related to the user.
You really need better docs on this relate stuff! 😉
-
March 17, 2017 at 10:41 am #7846
friendlyfire3MemberBump. Any suggestions on this?
-
-
AuthorPosts
- You must be logged in to reply to this topic.